Object-C,NumberDemo和StringDemo
晚上抽空寫了2個Demo.
第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
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
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