【Cocoa and Object-c : Up and Running 笔记】04 Object-C 基础语法

来源:互联网 发布:李世民 知乎 胡人 编辑:程序博客网 时间:2024/04/27 15:21
类标头
  1. @interface Human : NSObject 
  2. // Define properties and methds 
  3. @end 
类实现
  1. #import "Human.h"  
  2.  
  3. @interface Human ()  
  4. // Define private properties and methods  
  5. @end  
  6.  
  7. @implementation Human {  
  8. // Define private instance variables  
  9. }  
  10.  
  11. // Provide method implementation code  
  12.  
  13. @end  
 创建一个实例
  1. Human * anObject = [[Human alloc] init]; 
 
方法
定义方法
  1. // Returns nothing and has no arguments 
  2. - (void)foo;   
  3.  
  4. //Returns an NSString object and takes one argument of type NSObject  
  5. - (NSString *)fooWithArgument:(NSObject *)bar;  
  6.  
  7. //Takes two arguments one of type NSObject and a second one of type NSString  
  8. - (void)fooWithArgument:(NSObject *)bar andArgument:(NSString *)baz;  
  9.  
  10. // Defines a class method (note the + sign) 
  11. + (void)aClassMethod; 
实现方法
  1. - (NSString *)fooWithArgument:(NSObject *)bar{ 
  2.     //Do something here 
  3.     return retValue; 
调用方法
  1. [anObject someMethod]; 
  2. [anObject someMethodWithArg1:arg1 andArg2:arg2]; 
 
运算符
算术运算符
比较运算符
逻辑运算符
复合赋值运算符
位运算符
其他运算符
属性
定义属性
  1. @property (attribute1, attribute2) NSString *aProperty; 
 
访问属性
  1. [anObject aProperty]; 
  2. //Alternative 
  3. anObject.aProperty 
 
常量
预处理宏
这并不是一个真正的常量,因为它定义了一个宏在编译之前用真正的值代替所有出现的MAX_NUMBER_OF_ITEMS 
  1. #define MAX_NUMBER_OF_ITEMS 10 
使用const
一个更好的方法是使用const
  1. NSString *const kMyName = @"Clark"
Static和extern
如果你知道只能在实现文件中使用常量,那么你可以用static。使用static意味着这个常量只能在该文件中可用。
  1. static NSString * const kMyName = @"Clark"
如果你想定义一个全局常量,那么你应该使用extern。
  1. //.h file 
  2. extern NSString * const kMyName; 
  1. //.m file 
  2. NSString * const kMyName = @"Clark"
NSString
示例:
  1. NSString *firstName = @"Clark";  
  2. NSString *lastName = @"Kent";  
  3. NSString *fullName = [NSString stringWithFormat:  @"My full name is %@ %@",  firstName, lastName];  
 
NSString格式限定符
NSArray
示例:
  1. //Create an array 
  2. NSMutableArray *anArray = [@[@"Clark Kent", @"Lois Lane"] mutableCopy]; 
  3.  
  4. //Add new items 
  5. [anArray addObject:@"Lex Luthor"]; 
  6.  
  7. //Find array length 
  8. NSLog(@"Array has %d items", [anArray count]);  
  9.  
  10. //Iterate over array items 
  11. for (NSString *person in anArray) {  
  12.  NSLog(@"Person: %@", person);  
  13. }  
  14.  
  15. //Access item with index 
  16. NSString *superman = anArray[0]; 
  17.  
  18. //Remove Object @"Clark Kent" 
  19. [anArray removeObject:@"Clark Kent"]; 
  20.  
  21. //Remove the first Object 
  22. [anArray removeObjectAtIndex:0]; 
NSDictionary
示例:
  1. //Create a dictionary 
  2. NSMutableDictionary *person = [@{ 
  3.                              @"firstname" : @"Clark"
  4.                              @"lastname" : @"Kent"
  5.                              @"age" : [NSNumber numberWithInt:35] 
  6.                              } mutableCopy]; 
  7.  
  8. //Access values  
  9. NSLog(@"Superman's first name is %@", person[@"firstname"]); 
  10. //or 
  11. NSLog(@"Superman's first name is %@", [person objectForKey:@"firstname"]); 
  12.  
  13. //Find number of items in dicitonary 
  14. [person count]; 
  15.  
  16. // Add an object to a dictionary 
  17. [person setObject:@"job" forKey:@"teacher"]; 
  18.  
  19. //Remove an object to a dictionary 
  20. [person removeObjectForKey:@"firstname"]; 
 
枚举类型
苹果的示例:
每个枚举被赋予一个响应的整数值,所以
  1. typedef enum { 
  2.    UIButtonTypeCustom = 0, 
  3.    UIButtonTypeSystem, 
  4.    UIButtonTypeDetailDisclosure, 
  5.    UIButtonTypeInfoLight, 
  6.    UIButtonTypeInfoDark, 
  7.    UIButtonTypeContactAdd, 
  8.    UIButtonTypeRoundedRect, 
  9. } UIButtonType; 
与下述代码一样
  1. typedef enum { 
  2.    UIButtonTypeCustom = 0, 
  3.    UIButtonTypeSystem = 1, 
  4.    UIButtonTypeDetailDisclosure = 2, 
  5.    UIButtonTypeInfoLight = 3, 
  6.    UIButtonTypeInfoDark = 4, 
  7.    UIButtonTypeContactAdd = 5, 
  8.    UIButtonTypeRoundedRect = 6, 
  9. } UIButtonType; 
不要求明确地定义第一个枚举的值,并且它默认为0
 
使用枚举类型
  1. UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
或者创建一个变量传递到方法,像这样
  1. UIButtonType myButtonType = UIButtonTypeCustom; 
  2. UIButton *myButton = [UIButton buttonWithType:myButtonType]; 
由于它们不是对象,所以你必须打印枚举类型为整数
  1. UIButtonType myButtonType = UIButtonTypeRoundedRect; 
  2.  
  3. // Bad, will give you a warning and might even crash 
  4. NSLog(@"%@", myButtonType); 
  5.  
  6. // Good, will properly print the value as an integer 
  7. NSLog(@"%d", myButtonType); 
 
流程控制语句
If-else语句
  1. if (someCondition) { 
  2.     // Execute if the condition is true 
  3. else if (someOtherCondition) { 
  4.     // Execute if the other condition is true 
  5. else { 
  6.     // Execute if the none of the above conditions are true 
 三元运算符
  1. someCondition ? @"True" : @"False"
For循环
  1. for (int i = 0; i < totalCount; i++) { 
  2.     // Do something here 
 While循环
  1. while (someCondition) { 
  2.    // Do something here 
 Do While循环
  1. do { 
  2.     // Do something here 
  3. while (someCondition); 
Switch语句
  1. switch (aLabel) 
  2.     case kLabelA: 
  3.         // Execute this if matched 
  4.         break
  5.   
  6.      case kLabelB: 
  7.         // Execute this if matched 
  8.         break
  9.   
  10.      default
  11.         // Execute this if matched 
  12.         break

0 0
原创粉丝点击