TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
Dog.h- #import <Foundation/Foundation.h>
- @interface Dog:NSObject
- @end;
复制代码
Dog.m- #import "Dog.h"
- @implementation Dog
- - (NSString *)desctription
- {
- return @"This is a dog!";
- }
- @end
复制代码
main.m- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[])
- {
- @autoreleasepool{
- Dog * dog = [[Dog alloc] init];
- NSArray * array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",dog,nil];
- //数组元素可以是任意对象,数组中装的是元素的地址;
- //返回每个元素的discription值(遍历打印)
- NSLog(@"%@",array);//非连续的单词用双引号"This is a dog!"标识
- NSLog(@"%@",dog);
- //*************************************遍历方法之枚举器法
- NSEnumerator * enumerator = [array objectEnumerator];
- id obj;
- while(obj = [enumerator nextObject]){
- NSLog(@"%@",obj);
- }
- //*************************************快速枚举法
- for(id obj in array){
- NSLog(@"%@",obj);
- }
- //*************************************使用i值遍历
- NSUInteger length = [array count];
- obj = [array objectIndex:2];
- NSUinteger i;
- for(i = 0,i < [array count],i++){
- NSLog(@"%@",[array objectAtIndex:i]);
- }
- [dog release];
- [array release];
- }
- return 0;
- }
复制代码
|
|