Школа-студия разработки приложений для iOS. Лекция 1. Objective-C

  • Published on
    18-Jan-2015

  • View
    1.727

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Transcript

  • 1. iOS 1 Objective-C

2. , ? 3. , 4. iOS? 5. iOS 6. XCode 7. Mac OS Lion 8. Mac OS? Mac Hacintosh (VMWare,VirtualBox) 9. Apple Developer Program$99 AppStore 10. Objective-C 11. 1981 Object-Oriented Pre-Compiler (OOPC). StepStone. Tom Love & Brad Cox1986 Objective-C. Object-Oriented Programming, AnEvolutionary Approach (Brad Cox).1988 NeXT Objective-C1996 Apple NeXT. Objective-C Mac OS.2007 Objective-C iPhone OS 12. - - 13. http://ru.wikipedia.org/wiki/_(_) 14. int main(int argc, const char * argv[]){int a = 5;int *b = &a;*b = 7;printf("%d", a);}7 15. double add(double a, double b){return a + b;}int main(int argc, const char * argv[]){double a = 5.5;double b = 7;double *arr = malloc(2 * sizeof(double));arr[0] = a;arr[1] = b;printf("array [0] = %g, [1] = %g, add = %g", arr[0], arr[1], add(arr[0], arr[1]));free(arr);} 16. typedef struct{float x;float y;} Location;double add(double a, double b){return a + b;}Location createLocation(float x, float y){Location l;l.x = x;l.y = y;return l;}int main(int argc, const char * argv[]){Location l = createLocation(1.5, 0.5);printf("location: {%g, %g}", l.x, l.y);} 17. typedef enum {UIViewAnimationCurveEaseInOut,UIViewAnimationCurveEaseIn,UIViewAnimationCurveEaseOut,UIViewAnimationCurveLinear} UIViewAnimationCurve;UIViewAnimationCurve a = UIViewAnimationCurveLinear;printf("%d", a); 18. {int *a = malloc(sizeof(int) * 10);int a = 10;} free(a);{int a = 20;} 19. http://ru.wikipedia.org/wiki/-_ 20. - + - - - 21. :- ()- ()- (/)- ___ ()- _ ()- ( ) :- ()- () 22. ( ) : - ( ): - () 23. Objective-C 24. NSString *a = [[NSString alloc] init];NSString *b = [a copy];NSString *c = @"string";[b stringByReplacingOccurrencesOfString:@" " withString:@""];[c stringByPaddingToLength:20withString:@" " startingAtIndex:0]; 25. nil null , - nil - 0 - 26. nilNSString *a = nil;int length = [a length]; // 0NSString *m = [a mutableCopy]; // nilNSRange range = [a rangeOfString:@"a"]; // undefined!!!if (a) // if (a != nil){NSRange r = [a rangeOfString:@"a"];} 27. BOOLBOOL a = YES;if (a) // if (a == YES){BOOL b = [c isEqual:d];if (!b) // if (a == NO){//blabla}} 28. CGFloat a = 0.5;NSInteger b = -1;NSUInteger q = 10; 29. CGPoint p;p.x = 10;p.y = 20;p = CGPointMake(10, 20);CGSize s;s.width = 100;s.height = 100;s = CGSizeMake(100, 100);CGRect r;r.size = s;r.origin = p;r = CGRectMake(10, 20, 100, 100); 30. 31. NSStringNSMutableStringNSString *a = @"abc";NSString *b = [a stringByReplacingOccurrencesOfString:@"a" withString:@"b"];NSLog(@"b: %@", b);NSMutableString *m = [b mutableCopy];NSRange r;r.length = m.length;r.location = 0;[m replaceOccurrencesOfString:@"c" withString:@"b"options:0range:r];NSLog(@"m: %@", m); 32. NSArray NSMutableArray NSArray *a = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];NSString *first = [a objectAtIndex:0];NSString *last = [a objectAtIndex:[a count] - 1];NSMutableArray *b = [a mutableCopy];[b addObject:@"r"];[b replaceObjectAtIndex:1 withObject:@"q"];[b removeObjectAtIndex:2]; 33. NSDictionaryNSMutableDictionary -. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"a", @"key1", @"b", @"key2", nil];NSString *first = [dict objectForKey:@"key1"];NSMutableDictionary *m = [dict mutableCopy];[m setObject:@"c" forKey:@"key3"];[m setObject:@"aa" forKey:@"key1"];NSLog(@"m: %@", m); 34. NSSetNSMutableSet . containsNSSet *s = [NSSet setWithObjects:@"a", @"b", @"c", nil];BOOL c = [s containsObject:@"b"];NSMutableSet *m = [s mutableCopy];[m addObject:@"d"];[m removeObject:@"a"]; 35. (enumeration)NSArray *arr = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];for (NSString *a in arr){NSLog(@"%@", a);}NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"a", @"key1",@"b", @"key2",nil];for (NSString *key in dict){NSString *value = [dict objectForKey:key];NSLog(@"%@ - %@", key, value);} 36. NSNumberNSNumber *a = [NSNumber numberWithInt:3];int b = [a intValue];NSNumber *c = [NSNumber numberWithFloat:2.5f];float d = [c floatValue];NSNumber *e = [NSNumber numberWithDouble:3.567];double f = [e doubleValue];if ([a isEqualToNumber:c])NSLog(@"equals"); 37. NSDataNSMutableData , ..NSData *data = [[NSData alloc] initWithContentsOfFile:@"data.txt"];NSInteger length = [data length];[data writeToFile:@"result.txt" atomically:YES];NSMutableData *m = [data mutableCopy];[m appendData:data]; 38. NSNull NSMutableArray *a = [[NSMutableArray alloc] init];[a addObject:[NSNull null]];NSObject *q = [a lastObject];if (q == [NSNull null]){//bla bla} 39. NSValue CGPoint p = CGPointMake(10, 20);CGRect r = CGRectMake(10, 20, 30, 40);NSValue *v1 = [NSValue valueWithCGPoint:p];NSValue *v2 = [NSValue valueWithCGRect:r];NSArray *arr = [NSArray arrayWithObjects:v1, v2, nil];CGRect r2 = [[arr lastObject] CGRectValue]; 40. Dot notation Rectangle- oat width ()- oat height ()- Location location ()Rectangle *r = [[Rectangle alloc] init];[r setWidth:10];[r setHeight:20];Location l;l.x = 1;l.y = 2;[r setLocation:l];NSLog(@"%g, %g, (%g, %g)",[r width],[r height],[r location].x,[r location].y); 41. Dot notationRectangle *r = [[Rectangle alloc] init];r.width = 10;r.height = 20;Location l = {1, 2};r.location = l;NSLog(@"%g, %g, (%g, %g)",r.width,r.height,r.location.x,r.location.y); 42. 43. User.h 44. @interface UserUser.h@end 45. @interface User : NSObject User.h@end 46. @interface User : NSObject{ NSString *_name;} User.h@end 47. @interface User : NSObject{ NSString *_name;}- (void)setName:(NSString *)name; User.h- (NSString *)name;@end 48. @interface User : NSObject{ NSString *_name;}- (void)setName:(NSString *)name; User.h- (NSString *)name;@endUser.m 49. @interface User : NSObject{ NSString *_name;}- (void)setName:(NSString *)name; User.h- (NSString *)name;@end@implementation UserUser.m@end 50. @interface User : NSObject{ NSString *_name;}- (void)setName:(NSString *)name; User.h- (NSString *)name;@end@implementation User- (void)setName:(NSString *)name{}_name = name;User.m- (NSString *)name{return _name;}@end 51. User.h 52. User.h#import 53. User.h#import @interface User@end 54. User.h#import @interface User : NSObject@end 55. User.h#import @interface User : NSObject{ NSInteger _age;}@end 56. User.h#import @interface User : NSObject{ NSInteger _age;}@property(nonatomic, strong) NSString *name;@property(nonatomic) NSInteger age;@end 57. User.h#import @interface User : NSObject{ NSInteger _age;}@property(nonatomic, strong) NSString *name;@property(nonatomic) NSInteger age;- (void)setAge:(NSInteger)age;- (NSInteger)age;@end 58. User.h#import @interface User : NSObject{ NSInteger _age;}@property(nonatomic, strong) NSString *name;@property(nonatomic) NSInteger age;- (void)setAge:(NSInteger)age;- (NSInteger)age;- (void)deleteProfile;@end 59. User.h#import @interface User : NSObject{ NSInteger _age;}@property(nonatomic, strong) NSString *name;@property(nonatomic) NSInteger age;- (void)setAge:(NSInteger)age;- (NSInteger)age;- (void)deleteProfile;- (void)postCommentWithText:(NSString *)text;- (void)postCommentWithTopic:(NSString *)topic andText:(NSString *)text;@end 60. User.m 61. #import "User.h"@implementation User User.m@end 62. #import "User.h"@implementation UserUser.m@synthesize age = _age;@synthesize name = _name;@end 63. #import "User.h"@implementation UserUser.m@synthesize age = _age;@synthesize name = _name;- (void)setAge:(NSInteger)age{_age = age;NSLog(@" %d", age);}@end 64. #import "User.h"@implementation UserUser.m@synthesize age = _age;@synthesize name = _name;- (void)setAge:(NSInteger)age{_age = age;NSLog(@" %d", age);}- (void)deleteProfile{// NSString *name = [self name];NSLog(@" %@ ", name);}- (void)postCommentWithText:(NSString *)text{[self postCommentWithTopic:@"" andText:text];}- (void)postCommentWithTopic:(NSString *)topic andText:(NSString *)text{NSLog(@" %@ (: %d) %@", self.name, self.age, topic);}@end 65. Admin.h 66. Admin.h#import "User.h"@interface Admin : User- (void)deleteComment:(NSInteger)key;@end 67. Admin.h#import "User.h"@interface Admin : User- (void)deleteComment:(NSInteger)key;@endAdmin.m 68. Admin.h#import "User.h"@interface Admin : User- (void)deleteComment:(NSInteger)key;@endAdmin.m#import "Admin.h"@implementation Admin- (void)deleteComment:(NSInteger)key{// NSLog(@" %d ", key);// , [self postCommentWithTopic:@" " andText:@" "];} 69. 70. User *user = [[User alloc] init]; 71. User *user = [[User alloc] init];user.age = 34;[user setName:@"UserName"]; 72. User *user = [[User alloc] init];user.age = 34;[user setName:@"UserName"];NSString *comment = @"";[user postCommentWithText:comment];[user deleteProfile]; 73. 74. - (id)initWithName:(NSString *)name{self = [super init];if (self){self.name = name;}return self;} 75. - (id)initWithName:(NSString *)name{self = [super init];if (self){self.name = name;}return self;}- (id)init{return [self initWithName:@" "];} 76. 77. Admin.h: 78. Admin.h:+ (Admin *)createAdmin; 79. Admin.h:+ (Admin *)createAdmin; Admin.m: 80. Admin.h:+ (Admin *)createAdmin; Admin.m:+ (Admin *)createAdmin{Admin *admin = [[Admin alloc] initWithName:@" "];admin.age = 34;return admin;} 81. Admin.h:+ (Admin *)createAdmin; Admin.m:+ (Admin *)createAdmin{Admin *admin = [[Admin alloc] initWithName:@" "];admin.age = 34;return admin;}: 82. Admin.h: + (Admin *)createAdmin; Admin.m:+ (Admin *)createAdmin{Admin *admin = [[Admin alloc] initWithName:@" "];admin.age = 34;return admin;}:Admin *admin = [Admin createAdmin];[admin deleteComment:10]; 83. id 84. NSString *a = @"a";id b = a;if ([b respondsToSelector:@selector(isEqualToString:)]){@ BOOL e = [b isEqualToString:@"a"];}SEL s = @selector(removeAllObjects);NSMutableArray *arr = ...;[arr performSelector:s];[arr makeObjectsPerformSelector:s] 85. NSString *a = @"a";id b = a;if ([b isKindOfClass:[NSString class]]){@ BOOL e = [b isEqualToString:@"a"];} 86. 87. @protocol SendMessageProtocol- (void)sendMessage:(NSString *)message;@end 88. @protocol SendMessageProtocol- (void)sendMessage:(NSString *)message;@end@interface User : NSObject@end 89. @protocol SendMessageProtocol- (void)sendMessage:(NSString *)message;@end@interface User : NSObject@end@implementation User- (void)sendMessage:(NSString *)message{// send message}@end 90. @protocol SendMessageProtocol- (void)sendMessage:(NSString *)message;@end@interface User : NSObject@end@implementation User- (void)sendMessage:(NSString *)message{// send message}@endid sender = [[User alloc] init];[sender sendMessage:@"message"]; 91. 92. @interface NSString(User)- (User *)userWithName;@end 93. @interface NSString(User)- (User *)userWithName;@end@implementation NSString(User)- (User *)userWithName{User *result = [[User alloc] init];result.name = self;return result;}@end 94. @interface NSString(User)- (User *)userWithName;@end@implementation NSString(User)- (User *)userWithName{User *result = [[User alloc] init];result.name = self;return result;}@endUser *test = [@"Username" userWithName]; 95. retain/release ARC 96. retainCountNSMutableString *a = [[NSMutableString alloc] init];NSLog(@"%d", [a retainCount]);[a appendString:@"a"];[a retain];NSLog(@"%d", [a retainCount]);1[a release];NSLog(@"%d", [a retainCount]);[a release];2 1dealloc 97. retain setter- (void)setName:(NSString *)name{NSString *prev = _name;_name = name;[_name retain];[prev release];}- (void)dealloc{[super dealloc];[_name release];} 98. autorelease- (NSString *)fullName{NSMutableString *s = [[NSMutableString alloc] init];[s appendString:[self name]];[s appendString:@" "];[s appendString:[self secondName]];return [s autorelease];}- (void)main{NSString *name = [self fullName];NSLog(@"%@", name);} NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; ... [pool drain]; 99. retain/release init 1 retain-setter: retain , release - retain - release 100. ARC retainrelease autorelease[super dealloc] dealloc retain/release retain autorelease release 101. ARCNSString *a = [[NSString alloc] init];NSLog(@"%@", a);release- (void)setName:(NSString *)name{ [super dealloc];_name = name;} release retainrelease - (void)dealloc{NSLog(@"dealloc");} 102. CarWheel NSArray *_wheelsCar *carCar *car = [[[Car alloc] init] autorelease];Wheel *wheel = [[[Wheel alloc] init] autorelease];[wheel setCar:car];[car addWheel:wheel]; retainCount = 2 autorelease retainCount = 1 103. strong weak unsafe_unretained copy@interface Car : NSObject@property(strong, nonatomic) NSArray *wheels;@end@interface Wheel : NSObject@property(weak, nonatomic) Car *car;@end 104. Objective-C style guide : myLocalVariable : myProperty : MyClass : doSomethingWith: : _myLocal myLocal_ myLocal 105. 106. ? Objective-C? 107. "string1" @"string2" 108. 5 ? 109. ? 110. strong weak? ? 111. ! gleb34@gmail.com twitter.com/pilot34