0

重新来。我对目标 C 相当陌生。我创建了以下类,但我不知道如何初始化数组。

任何人都可以提供有关如何初始化 NSArray 的任何指导吗?

StatusPost.m

#import "StatusPost.h"


@implementation StatusPost

@synthesize messageId, fromName, friendId, message, choice2, choice3, choice4, picture,     fbImage, commentCount, commentArray;

-(id)initWithMessageId:(NSString*) rMessageId 
       fromName:(NSString*) rFromName
         friendId:(NSString*) rFriendId
        message:(NSString*) rMessage 
           choice2:(NSString*) rChoice2
           choice3:(NSString*) rChoice3
           choice4:(NSString*) rChoice4
           picture:(NSString *) rPicture
           fbImage:(UIImage *)rfbImage
      commentCount:(NSString*) rCommentCount
      commentArray:(NSArray*) rCommentArray
{
if (self = [super init]) {
    commentArray = [NSArray new];
    self.messageId = rMessageId;
    self.fromName = rFromName;
    self.friendId = rFriendId;
    self.message = rMessage;
    self.choice2 = rChoice2;
    self.choice3 = rChoice3;
    self.choice4 = rChoice4;
    self.picture = rPicture;
    self.fbImage = rfbImage;
    self.commentCount = rCommentCount;
    self.commentArray = rCommentArray;

}
return self;
}



- (void)dealloc {
[messageId release];
[fromName release];
[friendId release];
[message release];
[picture release];
[fbImage release];
[commentCount release];
[commentArray release];
[super dealloc];
}

@end

StatusPost.h:#import

@interface StatusPost : NSObject {
NSString* messageId;
NSString* fromName;
NSString* friendId;
NSString* message;
NSString* choice2;
NSString* choice3;
NSString* choice4;
NSString* picture;
UIImage* fbImage;
NSString* commentCount;
NSArray* commentArray;

}

@property (nonatomic, retain) NSString* messageId;
@property (nonatomic, retain) NSString* fromName;
@property (nonatomic, retain) NSString* friendId;
@property (nonatomic, retain) NSString* message;
@property (nonatomic, retain) NSString* choice2;
@property (nonatomic, retain) NSString* choice3;
@property (nonatomic, retain) NSString* choice4;
@property (nonatomic, retain) NSString* picture;
@property (nonatomic, retain) UIImage* fbImage;
@property (nonatomic, retain) NSString* commentCount;
@property (nonatomic, retain) NSArray* commentArray;


-(id)initWithMessageId:(NSString*) rMessageId 
       fromName:(NSString*) rFromName
         friendId:(NSString*) rFriendId
        message:(NSString*) rMessage
           choice2:(NSString*) rChoice2
           choice3:(NSString*) rChoice3
           choice4:(NSString*) rChoice4
           picture:(NSString*) rPicture
           fbImage:(UIImage*) rfbImage
      commentCount:(NSString*) rCommentCount
      commentArray:(NSArray*) rCommentArray;

@end
4

2 回答 2

0

您很可能从未初始化数组,因此当您尝试添加对象时,您只是向 nil 发送消息。在自定义类的init方法中,添加以下行:

commentArray = [NSMutableArray new];
于 2012-08-11T01:53:08.937 回答
0

[NSArray new]是 的简写[[NSArray alloc] init],因此,从技术上讲,该语句“初始化”了 NSArray。

但是,您的代码看起来有点奇怪。您的 中有以下语句init

commentArray = [NSArray new];
self.commentArray = rCommentArray;

第一条语句将实例变量设置commentArray为新分配/初始化的 NSArray 的地址,而第二条语句将属性设置commentArray为参数值。但是,您已经(通过@synthesize)使实例变量commentArray成为 property 的“后备存储” commentArray,因此当您执行第二行时,第一行的效果被覆盖(并且您创建的 NSArray 被“泄露”)。

(但如果你真正的问题是如何用值“加载”一个 NSArray,你应该问这个问题——你会得到不同的答案。)

于 2012-08-11T03:27:05.943 回答