Sian 发表于 2013-12-20 21:34:27

第十讲:Objective-C基本数据结构之NSArray

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 = [ init];

                NSArray * array = [ initWithObjects:@"One",@"Two",@"Three",dog,nil];
                //数组元素可以是任意对象,数组中装的是元素的地址;
                //返回每个元素的discription值(遍历打印)               

                NSLog(@"%@",array);//非连续的单词用双引号"This is a dog!"标识
                NSLog(@"%@",dog);
                //*************************************遍历方法之枚举器法
                NSEnumerator * enumerator = ;
                id obj;
                while(obj = ){
                        NSLog(@"%@",obj);
                }
                //*************************************快速枚举法
                for(id obj in array){
                        NSLog(@"%@",obj);
                }
                //*************************************使用i值遍历
                NSUInteger length = ;
                obj = ;
                NSUinteger i;
                for(i = 0,i < ,i++){
                        NSLog(@"%@",);
                }
                ;               
                ;
        }
        return 0;
}

Sian 发表于 2014-1-8 21:01:51

三个非常实用的遍历方法:
1、枚举器法
2、快速枚举器法
3、i值遍历法
Xcode代码如下:
//
//main.m
//NSArray
//
//Created by yusian on 14-1-8.
//Copyright (c) 2014年 yusian. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Dog.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
      
      Dog * dog = [ init];
      NSArray * array = [ initWithObjects:@"One",@"Two",@"Three",dog,nil];
      NSLog(@"%@",array);
      
      NSEnumerator * enumerator = ;
      id obj;
      while (obj = ) {
            NSLog(@"%@",obj);
      }//遍历法之枚举器法
      
      for (id obj in array) {
            NSLog(@"%@",obj);
      }//遍历法之快速枚举法
      
      NSUInteger length = ;
      for (int i = 0; i <length; i++) {
            NSLog(@"%@",);
      }//i值遍历法
      
      ;
      ;
    }
    return 0;
}

Sian 发表于 2014-1-8 21:02:15

Sian 发表于 2014-1-8 21:01
三个非常实用的遍历方法:
1、枚举器法
2、快速枚举器法


输出结果:
2014-01-08 20:59:44.789 NSArray (
    One,
    Two,
    Three,
    "This is a dog!"
)
2014-01-08 20:59:44.794 NSArray One
2014-01-08 20:59:44.795 NSArray Two
2014-01-08 20:59:44.795 NSArray Three
2014-01-08 20:59:44.795 NSArray This is a dog!
2014-01-08 20:59:44.796 NSArray One
2014-01-08 20:59:44.796 NSArray Two
2014-01-08 20:59:44.796 NSArray Three
2014-01-08 20:59:44.797 NSArray This is a dog!
2014-01-08 20:59:44.797 NSArray One
2014-01-08 20:59:44.797 NSArray Two
2014-01-08 20:59:44.798 NSArray Three
2014-01-08 20:59:44.798 NSArray This is a dog!
Program ended with exit code: 0
页: [1]
查看完整版本: 第十讲:Objective-C基本数据结构之NSArray