Upload
sheila-hart
View
251
Download
0
Embed Size (px)
Citation preview
1
iOS Programming
Xcode操作環境基本介紹
Xcode開始對話窗 版本 5.11
建立新專案
專案類型
基本專案類型
專案屬性設定
目標裝置類型
Xcode主要工作區畫面1.點選專案基本設定
3.檢查目標裝置類型
4.可支援姿態設定
2.目標作業系統版本
5.執行
專案導覽區
專案基本設定檔
Frameworks
畫面配置檔
App主要類別
ViewController對應類別
畫面配置Stroyboard
主要編輯區
ViewController屬性設定區
工具區
流程測試Seque起始 ViewController設定
Objective-C語言概論
Objective-C語言概論 變數及資料型態 條件與迴圈控制 輸出 NSLog
Objective-C類別的制定與使用– 類別宣告– 方法制定和使用
BOOL◦ BOOL isBool = YES;
◦ NSLog(@"%d", isBool);
◦ NSLog(@"%@", isBool ? @"YES" : @"NO");
char◦ char aChar = 'a';
◦ unsigned char anUnsignedChar = 255;
◦ NSLog(@"The letter %c is ASCII number %hhd", aChar, aChar);
◦ NSLog(@"%hhu", anUnsignedChar);
short◦ short aShort = -32768;
◦ unsigned short anUnsignedShort = 65535;
◦ NSLog(@"%hd", aShort);
◦ NSLog(@"%hu", anUnsignedShort);
int◦ int anInt = -2147483648;
◦ unsigned int anUnsignedInt = 4294967295;
◦ NSLog(@"%d", anInt);
◦ NSLog(@"%u", anUnsignedInt);
long◦ long aLong = -9223372036854775808;
◦ unsigned long anUnsignedLong = 18446744073709551615;
◦ NSLog(@"%ld", aLong);
◦ NSLog(@"%lu", anUnsignedLong);
long long◦ long long aLongLong = -9223372036854775808;
◦ unsigned long long anUnsignedLongLong = 18446744073709551615;
◦ NSLog(@"%lld", aLongLong);
◦ NSLog(@"%llu", anUnsignedLongLong);
變數Integer Types
float◦ float aFloat = -21.09f;◦ NSLog(@"%f", aFloat); ◦ NSLog(@"%8.2f", aFloat);
double◦ double aDouble = -21.09; ◦ NSLog(@"%8.2f", aDouble); ◦ NSLog(@"%e", aDouble);
long double◦ long double aLongDouble = -21.09e8L;◦ NSLog(@"%Lf", aLongDouble); ◦ NSLog(@"%Le", aLongDouble)
變數Floating-Point Types
Creating ◦ NSString *make = @"Porsche";◦ int year = 1968; ◦ NSString *message = [NSString stringWithFormat:@"That's a %@ %@ from %d!", make, model, year];
Enumerating◦ unichar letter = [make characterAtIndex:i];
Comparing ◦ NSString *car = @"Porsche Boxster"; ◦ if ([car isEqualToString:@"Porsche Boxster"]) { ◦ } ◦ if ([car hasPrefix:@"Porsche"]) { ◦ } ◦ if ([car hasSuffix:@"Carrera"]) { ◦ }
◦ NSString *otherCar = @"Ferrari"; ◦ NSComparisonResult result = [car compare:otherCar]; ◦ if (result == NSOrderedAscending) { ◦ } else if (result == NSOrderedSame) { ◦ } else if (result == NSOrderedDescending) ◦ }
變數NSString
Combining ◦ NSString *make = @"Ferrari"; ◦ NSString *model = @"458 Spider"; ◦ NSString *car = [make stringByAppendingString:model]; ◦ car = [make stringByAppendingFormat:@" %@", model];
Searching◦ NSString *car = @"Maserati GranCabrio"; ◦ NSRange searchResult = [car rangeOfString:@"Cabrio"]; ◦ if (searchResult.location == NSNotFound) { ◦ NSLog(@"Search string was not found"); ◦ } else { ◦ NSLog(@"'Cabrio' starts at index %lu and is %lu characters long", searchResult.location, searchResult.length); ◦ }
Subdividing ◦ NSString *car = @"Maserati GranTurismo"; ◦ NSLog(@"%@", [car substringToIndex:8]); ◦ NSLog(@"%@", [car substringFromIndex:9]); ◦ NSRange range = NSMakeRange(9, 4); ◦ NSLog(@"%@", [car substringWithRange:range]);
◦ NSString *models = @"Porsche,Ferrari,Maserati"; ◦ NSArray *modelsAsArray = [models componentsSeparatedByString:@","];◦ NSLog(@"%@", [modelsAsArray objectAtIndex:1]);
變數NSString
Replacing ◦ NSString *elise = @"Lotus Elise";◦ NSRange range = NSMakeRange(6, 5);◦ NSString *exige = [elise stringByReplacingCharactersInRange:range withString:@"Exige"];◦ NSString *evora = [exige stringByReplacingOccurrencesOfString:@“Exige” withString:@"Evora"];
Changing Case ◦ NSString *car = @"lotUs beSpoKE"; ◦ NSLog(@"%@", [car lowercaseString]); ◦ NSLog(@"%@", [car uppercaseString]); ◦ NSLog(@"%@", [car capitalizedString]);
Numerical Conversions◦ NSString *year = @"2012"; ◦ BOOL asBool = [year boolValue]; ◦ int asInt = [year intValue]; ◦ NSInteger asInteger = [year integerValue]; ◦ long long asLongLong = [year longLongValue]; ◦ float asFloat = [year floatValue]; ◦ double asDouble = [year doubleValue];
變數NSString
The id Type◦ id mysteryObject = @"An NSString object"; ◦ NSLog(@"%@", [mysteryObject description]); ◦ mysteryObject = @{@"model": @"Ford", @"year": @1967}; ◦ NSLog(@"%@", [mysteryObject description]);
The SEL Type◦ SEL someMethod = @selector(sayHello);
NSInteger and NSUInteger
Objective-C Primitives
Creating ◦ NSArray *germanMakes = @[@"Mercedes-Benz", @"BMW", @"Porsche", @"Opel", @"Volkswagen", @"Audi"];◦ NSArray *ukMakes = [NSArray arrayWithObjects:@"Aston Martin", @"Lotus", @"Jaguar", @"Bentley", nil]; ◦ NSLog(@"First german make: %@", germanMakes[0]); ◦ NSLog(@"First U.K. make: %@", [ukMakes objectAtIndex:0]);
Enumerating ◦ for (NSString *item in germanMakes) { ◦ NSLog(@"%@", item); ◦ } ◦ for (int i=0; i<[germanMakes count]; i++) { ◦ NSLog(@"%d: %@", i, germanMakes[i]); ◦ }
Membership Checking◦ if ([germanMakes containsObject:@"BMW"]) { ◦ NSLog(@"BMW is a German auto maker"); ◦ } ◦ NSUInteger index = [germanMakes indexOfObject:@"BMW"]; ◦ if (index == NSNotFound) { ◦ NSLog(@"Well that's not quite right..."); ◦ } else { ◦ NSLog(@"BMW is a German auto maker and is at index %ld", index); ◦ }
陣列NSArray
Creating◦ NSMutableArray *brokenCars = [NSMutableArray arrayWithObjects: @"Audi A6", @"BMW Z3", @"Audi
Quattro", @"Audi TT", nil];
Adding and Removing Objects◦ [brokenCars addObject:@"BMW F25"]; ◦ [brokenCars removeLastObject]; ◦ [brokenCars insertObject:@"BMW F25" atIndex:0]; ◦ [brokenCars removeObjectAtIndex:0];◦ [brokenCars removeObject:@"Audi Quattro"];◦ [brokenCars replaceObjectAtIndex:1 withObject:@"Audi Q5"];
可變陣列NSMutableArray
Creating NSDictionary *inventory = @{ @"Mercedes-Benz SLK250" : [NSNumber numberWithInt:13], @"Mercedes-Benz E350" : [NSNumber numberWithInt:22], @"BMW M3 Coupe" : [NSNumber numberWithInt:19], @"BMW X6" : [NSNumber numberWithInt:16], };
inventory = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:13], @"Mercedes-Benz SLK250", [NSNumber numberWithInt:22], @"Mercedes-Benz E350", [NSNumber numberWithInt:19], @"BMW M3 Coupe", [NSNumber numberWithInt:16], @"BMW X6", nil];
NSArray *models = @[@"Mercedes-Benz SLK250", @"Mercedes-Benz E350", @"BMW M3 Coupe", @"BMW X6"];
NSArray *stock = @[[NSNumber numberWithInt:13], [NSNumber numberWithInt:22], [NSNumber numberWithInt:19], [NSNumber numberWithInt:16]];
inventory = [NSDictionary dictionaryWithObjects:stock forKeys:models]; NSLog(@"%@", inventory);
NSDictionary
Accessing Values and KeysNSLog(@"There are %@ X6's in stock", inventory[@"BMW X6"]);
NSLog(@"There are %@ E350's in stock", [inventory objectForKey:@"Mercedes-Benz E350"]);
NSArray *outOfStock = [inventory allKeysForObject: [NSNumber numberWithInt:0]];
NSLog(@"The following cars are currently out of stock: %@", [outOfStock componentsJoinedByString:@", "]);
NSDictionary
Creating NSMutableDictionary *jobs = [NSMutableDictionary dictionaryWithDictionary:@{ @"Audi TT" : @"John", @"Audi Quattro (Black)" : @"Mary", @"Audi Quattro (Silver)" : @"Bill", @"Audi A7" : @"Bill" }];
Adding and Removing Entries[jobs setObject:@"Mary" forKey:@"Audi TT"]; [jobs removeObjectForKey:@"Audi A7"]; jobs[@"Audi R8 GT"] = @"Jack";
Combining DictionariesNSDictionary *bakerStreetJobs = @{ @"BMW 640i" : @"Dick", @"BMW X5" : @"Brad" }; [jobs addEntriesFromDictionary:bakerStreetJobs];
NSMutableDictionary
條件與迴圈控制 條件控制語法和C語言相同:
if(condition){…} if(condition){…}else{…} switch(condition){…}
三種迴圈形式與C語言相同: for(…;…;…){…} while(condition){…} do{…} while(condition);
輸出 NSLog 雖然仍可以使用 printf但在 Objective-C中建議使用 NSLog, 語法與 printf類似,但字串前需加上@用以代表是 NSString型態。◦ printf("Hello World %d",x);◦ NSLog(@"Hello World %d",x);
#import 在 Objective-C中建議使用#import取代#include,#import具有修正重複引入的功能。
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
建立類別
類別的制定與宣告
選擇 Objective-C Class
類別名稱
父類別
類別的制定與宣告
類別制定範例#import <Foundation/Foundation.h>@interface MyObject : NSObject{ int a,b,c; NSString *str;}-(void)sayHello;@end
@implementation MyObject-(void)sayHello{ NSLog(@"Hello");}@end
MyObject *obj = [[MyObject alloc] init]; [obj sayHello];
宣告
實作
建構和使用
事件控制範例
事件控制範例
一個 Objective-C類別的製定
@interface MyObject : NSObject @end
MyObject.h
@implementation MyObject@end MyObject.m
其中 .h檔用來宣告類別的成員變數和方法, .m檔則為方法的實作。
加上成員變數的範例@interface MyObject : NSObject{ int i; NSString *str;}@end
加上方法宣告和實作-(void)setUserName:(NSString*)name withAge:(int)age andGender:(NSString*)gender;
-(void)setUserName:(NSString*)name withAge:(int)age andGender:(NSString*)gender{ NSLog(@"Name:%@,Age:%d,Gender:%@",name,age,gender);}
MyObject *obj = [[MyObject alloc] init];[obj setUserName:@"John" withAge:30 andGender:@"M"];
宣告
實作
使用
標準物件建構方式MyObject *myObj= [[MyObject alloc] init]; -(id)init{ self = [super init]; return self;}
-(void)dealloc{ [super dealloc];}
@property @property可用來產生變數的 Getter和 Setter方法 如果@property 名稱和成員變數相同,在類別中的@property變數會自動轉成 _變數名稱
除非加上@synthesize指令
@interface MyObject : NSObject{NSString *str;} @property (strong,nonatomic) NSString *str;@end
@synthesize str=_str;