Loving Coding & Visual Design
Obj-C初学的几个要点
打算复习一下Objective-C,再学习了一下Try Objective-C,总结它讲的一些要点。
NSNumber相加要换成NSUInteger:
NSUInteger higgiesAgeInt = [higgiesAge unsignedIntegerValue];
NSLog(@"Higgie is actually %lu years old.", higgiesAgeInt );
初始化:
NSString *emptyString = [[NSString alloc] init];
NSString *copy = [[NSString alloc] initWithString:otherString];
Block
void (^logMessage)(void) = ^{
NSLog(@"Hello from inside the block");
};
void (^sumNumbers)(NSUInteger, NSUInteger) = ^(NSUInteger num1, NSUInteger num2){
NSLog(@"The sum of the numbers is %lu", num1 + num2);
};
Class示例
####MyFirstObjcClass.h
@interface MyFirstObjcClass : NSObject{
NSString *_othervalue;
}
@property NSString *firstName;
@property (readonly) NSString *lastName;
- (void) changeLastName:(NSString *)newLastName;
- (MyFirstObjcClass *) initWithFirstName:(NSString *)firstName
lastName:(NSString *)lastName;
@end
####MyFirstObjcClass.m
#import "MyFirstObjcClass.h"
@implementation MyFirstObjcClass
- (MyFirstObjcClass *) init;
{
NSLog(@"Cool, a new class is being initialized");
_firstName = @"Duo";
_lastName = @"Zhai";
return [super init];
}
- (MyFirstObjcClass *) initWithFirstName:(NSString *)firstName
lastName:(NSString *)lastName;
{
_firstName = firstName;
_lastName = lastName;
return [super init];
}
- (void) changeLastName:(NSString *)newLastName;
{
_lastName = newLastName; //下划线开头的Class方法内部变量
}
@end
实例化:
MyFirstObjcClass *person = [[MyFirstObjcClass alloc] initWithFirstName:@"Duo" lastName:@"Zhai"];
NSNumber相加要换成NSUInteger:
NSUInteger higgiesAgeInt = [higgiesAge unsignedIntegerValue];
NSLog(@"Higgie is actually %lu years old.", higgiesAgeInt );
初始化:
NSString *emptyString = [[NSString alloc] init];
NSString *copy = [[NSString alloc] initWithString:otherString];
Block
void (^logMessage)(void) = ^{
NSLog(@"Hello from inside the block");
};
void (^sumNumbers)(NSUInteger, NSUInteger) = ^(NSUInteger num1, NSUInteger num2){
NSLog(@"The sum of the numbers is %lu", num1 + num2);
};
Class示例
####MyFirstObjcClass.h
@interface MyFirstObjcClass : NSObject{
NSString *_othervalue;
}
@property NSString *firstName;
@property (readonly) NSString *lastName;
- (void) changeLastName:(NSString *)newLastName;
- (MyFirstObjcClass *) initWithFirstName:(NSString *)firstName
lastName:(NSString *)lastName;
@end
####MyFirstObjcClass.m
#import "MyFirstObjcClass.h"
@implementation MyFirstObjcClass
- (MyFirstObjcClass *) init;
{
NSLog(@"Cool, a new class is being initialized");
_firstName = @"Duo";
_lastName = @"Zhai";
return [super init];
}
- (MyFirstObjcClass *) initWithFirstName:(NSString *)firstName
lastName:(NSString *)lastName;
{
_firstName = firstName;
_lastName = lastName;
return [super init];
}
- (void) changeLastName:(NSString *)newLastName;
{
_lastName = newLastName; //下划线开头的Class方法内部变量
}
@end
实例化:
MyFirstObjcClass *person = [[MyFirstObjcClass alloc] initWithFirstName:@"Duo" lastName:@"Zhai"];
最 近 文 章
- Javascript循环的优先级 - Mon, 30 Nov -0001 00:00:00 +0000
- editplus 配置 yui 压缩 - Mon, 30 Nov -0001 00:00:00 +0000
- 关注的JS代码库 - Mon, 30 Nov -0001 00:00:00 +0000
- svn 出错:Directory is missing - Mon, 30 Nov -0001 00:00:00 +0000
- Unit Test - Mon, 30 Nov -0001 00:00:00 +0000
- PHPDoc - Mon, 30 Nov -0001 00:00:00 +0000
- yii框架快速入门 - Mon, 30 Nov -0001 00:00:00 +0000
- IE下前端开发之痛的根源 - Mon, 30 Nov -0001 00:00:00 +0000
- 一些PHP的错误解决办法 - Mon, 30 Nov -0001 00:00:00 +0000
- 我看PHP Frameworks - Mon, 30 Nov -0001 00:00:00 +0000