黑马程序员--ios基础

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">ios培训</a>、期待与您交流! ----------

1 数组

1.1 声明
NSArray *array;
1.2 数组的生成
NSArray *array;
array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSMutableArray *array = [NSMutableArray array]; // 声明可修改的数组同时对它赋值
1.3 数组元素的引用和赋值
// 元素的引用
[array objectAtIndex:0];
[array objectAtIndex:1];
// 赋值(NSMutableArray的实例)
[array removeObjectAtIndex:1]; // 删除一个元素
[array insertObject:@"1" atIndex:1]; // 在删除的地方插入一个元素
1.4 数组的长度
int array_num = [array count];

2 字典

2.1 声明
NSDictionary *dictinary;
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
 @"value1",@"key1",
@"value2",@"key2",
nil,
];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; // 声明可修改的字典的同时对它赋值
2.2 字典元素的引用和赋值
//元素的引用
id val1 = [dictionary objectForKey:@"key1"];
id val2 = [dictionary objectForKey:@"key2"];
// 赋值(NSMutableDictionary的实例)
[dictionary setObject:@"value1" forKey:@"key1"];
2.3 字典的操作

・获得Key

NSArray *keys = [dictionary allkeys]

・获得值

NSArray *values = [dictionary allValues];

・删除Key(NSMutableDictionary的实例)

[dictionary removeObjectForKey:@"key1"];

3 控制语句

if语句
if ( 条件 ) {
}
if ~ else语句
if (条件) {
} else {
}
if ~ else if 语句
if ( 条件 ) {
} else if ( 条件 ) {
}
while语句
int i = 0;
while (i < 5) {
// 处理
i++;
}
for语句
for (int i = 0; i < 5; i++) {
//处理
}

4 函数定义

虽然大多数情况下是用类中的方法来定义函数,但也可以用C语言的函数定义方法。
另外,还有作为对现有类的扩张的方法,称之为Category。

@interface ClassA : NSObject {
NSString *name_;
}
@property (nonatomic, copy) NSString *name_;
@implementation ClassA
@synthesize name_
- (void) helloWorld:(NSString *)name {
NSLog(@"hello %@ world! from %@", name, self.name_);
}
@end

5 文件的输入输出

NSString *inputFilePath = [INPUTFILEPATH stringByExpandingTildeInPath];
NSString *outputFilePath = [OUTPUTFILEPATH stringByExpandingTildeInPath];
NSFileManager *fm = [NSFileManager defaultManager];
if ( ![fm fileExistsAtPath:outputFilePath] ) {
[fm createFileAtPath:outputFilePath contents:nil attributes:nil];
}
NSFileHandle *input = [NSFileHandle fileHandleForReadingAtPath:inputFilePath];
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:outputFilePath];
@try {
NSData *data;
while( (data = [input readDataOfLength:1024]) && 0 < [data length] ){
[output writeData: data];
}
} @finally {
[input closeFile];
[output closeFile];
}

以下语法特性,读者如果知道的话会更好:

高速枚举

使用NSArray或NSDictionar的高速枚举法可以简单地枚举元素

NSArray *array = [NSArray arrayWithObjects:@"A", @"B", @"C", nil];
for (id i in array) {
//一些处理
}
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
 @"value1", @"key1",
@"value2", @"key2",
@"value3", @"key3",
nil
];
// 用key来做循环的场合
for (id i in [dictionary keyEnumerator]) {
// 一些处理
}
// 用值来做循环的场合
for (id i in [dictionary objectEnumerator]) {
// 一些处理
}

用临时变量id来做是可以的,不过指明Key的类型这种方法也不错。

for (NSString *k in [dictionary keyEnumerator]) {
 // 处理
}

Category
Category可以在已有的类中追加一个方法

@interface NSString (Decorate)
// 定义属于Category的方法
- (NSString *) decorateWithString:(NSString *)string;
@end
@implementation NSString (Decorate)
- (NSString *) decorateWithString:(NSString *)string {
return [NSString stringWithFormat:@"%@%@%@", string, self, string];
}
@end
NSLog(@"test: %@",[@"[MSG]" decorateWithString:@"**"]); // **[MSG]**

Protocol
类如果声明为符合某种Protocol的话,就要按照Protocol所规定的方法来实现这个类。Protocol和Java中的Interface类似。
Property
Property提供了访问类中成员变量的一种方法。
虽然在使用的时候你应该知道更多关于它的知识,但在这里我们只举一个例子。

@property (nonatomic, retain) NSArray *array_;

如果要对像上面那样声明retain的property赋值的话,如果不通过accessor来访问,那么就无法retain。

- (id)initWithParam:(NSArray *)array {
if (self = [super init]) {
array_ = array; // 无法retain
self.array_ = array; // 可以retain
}
return self;
}
- (void)dealloc {
[array_ release];
[super dealloc];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值