Object-C 基础学习(三)

12.enumerate with bolcks(快速循环代码块):

for example:

NSArray *newHats = @[@"Cowboy", @"Conductor", @"Baseball", 

  @"Beanie", @"Beret", @"Fez"];


//普通的循环
for (NSString *hat in newHats) { NSLog(@"Trying on hat %@", hat); }
//enumerate with blocks [newHats enumerateObjectsUsingBlock:
^(NSString *hat,NSUInteger index,BOOL *stop){//hat代表循环所得值,index代表所循环的位置,the arguement of stop that allow you stop enumerate array //if you want NSLog(@"Trying on hat %@", hat); }];

13.Introducing classes(类):

 Class Interface:

@interface MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone : NSObject//继承自NSObject

//属性

@property NSString *phoneName;

@property NSString *modelNumber;

@end

14.Implements Class:

Header file<ClassName>.h(头文件)

@interface Person : NSObject

@property NSString *firstName;

@property NSString *lastName;

@end

Implements file<ClassName>.m(实现文件)

#import "Person.h" //首先先将Person接口文件引入



@implementation Person //实现Person类 @end

15.Using a property

MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone.c

@interface MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone : NSObject



@property NSString *phoneName;

@property NSString *modelNumber;



@end

MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone.h

#import "MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone.h"



@implementation MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone

@end

Challenge.m

#import "MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone.h"

//初始化MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone类
MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone *talkingiPhone = [[MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone alloc] init]; 
talkingiPhone.phoneName
= @"123456"; //为属性赋值,实际上是调用了[talkingiPhone setPhoneName:@"123456"];

NSLog(@"%@", talkingiPhone.phoneName);

16.Create a custome method:

 MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone.h

@interface MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone : NSObject



@property NSString *phoneName;

@property NSString *modelNumber;



-(void) speak;//定义的方法,以”-“开头,void代表返回类型



@end

MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone.m

#import "MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone.h"



@implementation MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone



-(void)speak;//实现speak方法

{

  NSLog(@"Pouring coffee");

}



@end

17.Running custome method(从上):

Challenge

#import "MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone.h"



MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone *talkingiPhone = [[MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone alloc] init];

talkingiPhone.phoneName = @"Mr. Higgie";



[talkingiPhone speak];//调用MrHiggieIsAnAmazingPhoneAndIsLovedByEveryone类中自定义的speak方法

你可能感兴趣的:(object)