OBJECTIVE-C 基礎教學2/5 codeschool




OBJECTIVE-C 基礎教學2/5 codeschool Objects and Classes
//物件名稱 訊息傳遞名稱,這邊是呼叫物件的方法
//先不管物件是什麼,先記起這個重要的格式
[objectName messageName];
//實例
[tryobjc completeThisChallenge];


----------


/*物件是自行創造的
例如宣告NSString, NSNumber,  NSArray
*/
//宣告陣列然後印出陣列
NSArray *foods = @[@"tacos", @"burgers"];
NSLog(@"%@", foods);

//- (NSString *)description字串訊息傳遞的方法
//所以將物件result印出,呼叫需要描述foods的內容
//見下一題
NSArray *foods = @[@"tacos", @"burgers"];             
NSString *result = [foods description];
NSLog(@"%@", result);


----------


//例如呼叫city物件 需要的是length 要印出時則要使用lu型別
NSString *city = @"Ice World";
NSUInteger cityLength = [city length];
NSLog(@"City has %lu characters", cityLength);


----------



//Objective-c有許多與其他程式語言不同的地方
//例如下面這種寫法要將兩個數字乘起來,但在objc是無法計算

NSNumber *higgiesAge = @6;
NSNumber *phoneLives = @3;

NSNumber *product = higgiesAge * phoneLives;
//以上是錯誤的示範
NSLog(@"Higgie is actually %@ years old.", product);
//以上是錯誤的示範

//需要將型別先行轉換

NSUInteger higgiesAgeInteger = [higgiesAge unsignedIntegerValue];//轉無號整數

NSUInteger phoneLivesInt = [phoneLives unsignedIntegerValue];//轉無號整數

NSUInteger higgiesRealAge = higgiesAgeInt * phoneLivesInt;//進行相乘

//最後完整程式碼
//最後印出時需要將型別改為long unsigned型別%lu

NSNumber *higgiesAge = @6;
NSNumber *phoneLives = @3;

NSUInteger higgiesAgeInt = [higgiesAge unsignedIntegerValue];
NSUInteger phoneLivesInt = [phoneLives unsignedIntegerValue];

NSUInteger higgiesRealAge = higgiesAgeInt * phoneLivesInt;
NSLog(@"Higgie is actually %lu years old.", higgiesRealAge);


----------

//[firstName stringByAppendingString:lastName];
//這是字串

NSString *firstName = @"dairs";
NSString *lastName =@"jobs";

NSString *fullName = [firstName stringByAppendingString:lastName];
NSLog(@"%@", fullName);

//進階寫法

NSString *string1 = @"iphone";
NSString *string2 = [[NSString alloc] initWithFormat:@"nice"];

string1 = [[string1 stringByAppendingString:@"is"] stringByAppendingString:string2];

NSLog(@"%@",string1);

    //the answer is "iphoneis nice" 

//增加空格就是在附加的內部增加一個@" ",再繼續串接
NSString *firstName = @"dairs";
NSString *lastName =@"jobs";

NSString *fullName = [[firstName stringByAppendingString:@" "] stringByAppendingString:lastName];

NSLog(@"%@", fullName);


//fullName印出的結果會是daris jobs
/*在replaced裡面的
fullName stringByReplacingOccurrencesOfString:firstName                                                      withString:lastName 意思是以lastName替代fullname裡面的firstName所代表的字,所以daris會被替換成jobs
結果就變成jobs jobs*/

NSString *firstName = @"daris";
NSString *lastName =@"jobs";

NSString *fullName = [[firstName stringByAppendingString:@" "]
                        stringByAppendingString:lastName];

NSString *replaced = [fullName stringByReplacingOccurrencesOfString:firstName
                                                         withString:lastName];

NSLog(@"%@", replaced);


/*NSString *copy = [NSString stringWithString:firstName];
stringWithString:firstName代表複製firstName給copy物件*/

NSString *firstName = @"dairs";

NSString *copy = [NSString stringWithString:firstName];

NSLog(@"%@ is a copy of %@", copy, firstName);

/*allo=allocate意思是開始分派記憶體
但是當你沒有宣告init=initilize(初始化) 時,記憶體不會創造空間容納物件,所以必須先以[NSString alloc] 分派記憶體,
至少必須先創造一個空物件
NSString *emptyString = [[NSString alloc] init];
NSArray *emptyArray = [[NSArray alloc] init];
NSDictionary *emptyDictionary = [[NSDictionary alloc] init];

或者一開始直接分派資料進記憶體
NSString *copy = [[NSString alloc] initWithString:otherString];*/

NSString *firstName = @"dairs";

NSString *copy = [[NSString alloc] initWithString:firstName];

NSLog(@"%@ is a copy of %@", copy, firstName);

//簡單說其實有更簡單的寫法
//就是利用stringWithFormat:@"%@ %@"
//類似於NSLog(@"%@ %@", firstName, lastName);
//可以取代stringWithFormat:@"%@ %@"
//stringWithFormat:@"%@ %@"這是類別方法
//stringByAppendingString  這是物件方法

NSString *firstName = @"dairs";
NSString *lastName =@"jobs";

NSString *fullName = [NSString stringWithFormat:@"%@ %@",firstName,lastName];

NSLog(@"%@", fullName);












Comments

Popular posts from this blog

MEGA 暫存檔案刪除

IOS GCD多執行緒

利用CMD指令強制刪除