第1个是演示字符串,第2个是演示数组和可变数组。
只用到了几个基本的方法和基本语句。
NSString字符串对象,NSNumber和NSMutableNumber,if语句,NSLog方法。
使用Mac学习Object-C,感觉特别辛苦。
编程语法很简单,就是Mac的键盘按键、快捷方式、鼠标滚动,与Windows和HP电脑很不匹配。
另外,Object-C最大的坑是,方法调用,[a add b]用“中括号”表示方法调用,够奇葩的啊。
你大爷的~擦~
// // main.m // StringDemo // // Created by fansunion on 15/11/16. // Copyright (c) 2015年 demo. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSString *str1 =@"FansUnion"; NSString *str2 =@"XiaoLei"; NSString *str3 =@"XiaoLei"; //计算字符串的长度 NSLog(@"str1 length %lu",[str1 length]); //验证2个字符串是否相等 if([str1 isEqualToString:str2]){ NSLog(@"str1 == str2"); }else{ NSLog(@"str1 != str2"); } if([str2 isEqualToString:str3]){ NSLog(@"str2 == str3"); }else{ NSLog(@"str2 != str3"); } } return 0; }
2015-11-16 21:08:20.723 StringDemo[2139:127831] str1 length 9
2015-11-16 21:08:20.724 StringDemo[2139:127831] str1 != str2
2015-11-16 21:08:20.724 StringDemo[2139:127831] str2 == str3
Program ended with exit code: 0
// // main.m // NumberDemo // // Created by fansunion on 15/11/16. // Copyright (c) 2015年 demo. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //不可变数组 NSArray *names =[NSArray arrayWithObjects: @"FansUnion",@"XiaoLei",nil]; NSLog(@"NSArray!"); int i = 0; for(i=0;i<2;i++){ NSLog(@"%@",[names objectAtIndex:i]); } //可变数组 NSMutableArray *numbers =[NSMutableArray array]; NSNumber *myNumber; for(i=0;i<10;i++){ myNumber=[NSNumber numberWithInteger:i]; [numbers addObject:myNumber]; } //显示数组中的值 for(i=0;i<10;i++){ NSLog(@"%@",[numbers objectAtIndex:i]); } } return 0; }
2015-11-16 21:22:04.533 NumberDemo[2218:131384] NSArray!
2015-11-16 21:22:04.534 NumberDemo[2218:131384] FansUnion
2015-11-16 21:22:04.535 NumberDemo[2218:131384] XiaoLei
2015-11-16 21:22:04.535 NumberDemo[2218:131384] 0
2015-11-16 21:22:04.535 NumberDemo[2218:131384] 1
2015-11-16 21:22:04.535 NumberDemo[2218:131384] 2
2015-11-16 21:22:04.535 NumberDemo[2218:131384] 3
2015-11-16 21:22:04.536 NumberDemo[2218:131384] 4
2015-11-16 21:22:04.536 NumberDemo[2218:131384] 5
2015-11-16 21:22:04.536 NumberDemo[2218:131384] 6
2015-11-16 21:22:04.536 NumberDemo[2218:131384] 7
2015-11-16 21:22:04.536 NumberDemo[2218:131384] 8
2015-11-16 21:22:04.536 NumberDemo[2218:131384] 9
Program ended with exit code: 0