Object-C,NumberDemo和StringDemo

小雷FansUnion發表於2015-11-16
晚上抽空寫了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 <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




相關文章