1

我发现的最佳相关讨论:在 Objective-C 中制作对象数组。. 浏览了我准备这篇文章时出现的链接。

自学模式,创建迷你应用程序来强化和扩展我在书籍教程中获得的想法。………………。

目标:将一些对象(例如类“Trip”)存储在一个可变数组中,包含在另一个对象(例如类“TourCompany”)中。

(两个不同的类接口文件的一部分)

    @interface Trip : NSObject
    @property (strong, nonatomic) NSString *travelToLocation;

……</p>

        @interface TourCompany : NSObject
        @property (strong, nonatomic) NSMutableArray *trips ;

...... 代码和目标......

1) 证明我创建了 3 个行程对象并且可以访问 travelToLocation ivar ....

        Trip *trip0 = [[Trip alloc] init];
        Trip *trip1 = [[Trip alloc] init];
        Trip *trip2 = [[Trip alloc] init];

        [trip0 setTravelToLocation:@"Vienna"];
        [trip1 setTravelToLocation:@"Mt. St. Helens"];
        [trip2 setTravelToLocation:@"Tenochtitlan"];

        NSString *somePlace1 = trip1.travelToLocation;

        NSLog(@" %@ %@, \n \t Let's LOOK at one particular trip ivar %@.", twoBlanks, nuLn, somePlace1);

     //  CONSOLE RESULTS
      //     Let's LOOK at one particular trip ivar Mt. St. Helens.

.... 2) 证明我
i) 可以将这 3 个行程对象存储在本地声明的 NSMutable 数组中 ii) 可以访问 travelToLocation ivar ....

     // put trip objects into a LOCALLY DECLARED mutable array
        NSMutableArray *localTripsArray = [NSMutableArray array ];

      //[myArray addObject: someOtherPerson]; - compare to a STackOverflow discussion

        [localTripsArray addObject:trip0];
     [localTripsArray addObject:trip1];
        [localTripsArray addObject:trip2];


        for (Trip *t in localTripsArray) {

            NSLog(@"\n \t ^^^Trip location in LOCAL TRIPS Mutable ARRAY is %@", t.travelToLocation );

     }



    /*
        CONSOLE RESULTS
     objectInMutableArrayMiniAppChallenge[521:f803] 
        ^^^Trip location in LOCAL TRIPS Mutable ARRAY is Vienna
2012-07-05 13:47:17.172 objectInMutableArrayMiniAppChallenge[521:f803] 
        ^^^Trip location in LOCAL TRIPS Mutable ARRAY is Mt. St. Helens
        2012-07-05 13:47:17.172 objectInMutableArrayMiniAppChallenge[521:f803] 
         ^^^Trip location in LOCAL TRIPS Mutable ARRAY is Tenochtitlan

        */

.... 3) 但我不知道为什么我不将这 3 个对象存储在我的 TourCompany 类 ('friendlySkiesTourCo') 的实例中的 NSMutable 数组 ('trips') ... // 现在在我的实例中实例化一个对象旅游公司类

    TourCompany *friendlySkiesTourCo = [[TourCompany alloc] init];

 // put those same objects into the trips ivar in that object
 //[myArray addObject: someOtherPerson]; 
    //     - compare to a STackOverflow discussion
    //only difference I see is that instead of locally declared 'myArray'
    //   I'm using an ivar that's a member of an object
 // I know there's are values in those objects, because I displayed them above


 [friendlySkiesTourCo.trips addObject:trip0];
    [friendlySkiesTourCo.trips addObject:trip1];
    [friendlySkiesTourCo.trips addObject:trip2];


    for (Trip *t in friendlySkiesTourCo.trips) {

NSLog(@"\n \t +++ TourCompany 类对象 'friendlySkiesTourCo' 对象中的 'trips' 可变数组中的行程位置是 %@", t.travelToLocation );

    }


    /* Console NON-Results

    I never get any output from this NSLog directive
     I'll bet there is something pretty basic I don't understand
     No doubt, it's in the Gol Durned Manual online , but I'm not seeing it
     */

.... 4. 让我们仔细检查一下问题是否可能是设置类型问题 ....
// 我认为我并没有正确地将对象放入友好天空对象中的这个特定可变数组中 // 让我们只是检查

 int howManyTripsInMutableArray = [friendlySkiesTourCo.trips count];

 NSLog(@"\n \tThere are %d trips for the friendlySkiesTour Co ", howManyTripsInMutableArray);

        /*

     Sure enough...

     2012-07-05 13:53:53.235 objectInMutableArrayMiniAppChallenge[555:f803] 
    There are 0 trips for the friendlySkiesTour Co 

    */

感谢您提供任何线索。我希望我简洁地完成了这篇文章,并且您不会认为我只是没有 RTGDM(如果您想知道的话,那是'gol durn'),因为我确实尝试过。

月桂树

4

2 回答 2

3

我敢打赌你没有初始化friendlySkiesTourCo.trips。IE

friendlySkiesTourCo.trips = [[NSMutableArray alloc] init];

编辑

正如@Chuck 在下面的评论中指出的那样,这种分配实际上应该在类的初始化方法中完成:

@implementation TourCompany

-(id) init
{
    self = [super init];

    self.trips = [[NSMutableArray alloc] init];

    return self;
}
于 2012-07-06T17:02:44.240 回答
0

这有帮助吗?

trips =  [NSMutableArray arrayWithCapacity:3];
trip *trip1 = [[trip alloc] init];
trip *trip2 = [[trip alloc] init];
trip *trip3 = [[trip alloc] init];

trip1.traveltolocation = @"vienna";

[trips addObject:trip1];
[trips addObject:trip2];
[trips addObject:trip3];
于 2012-07-06T17:02:20.287 回答