【问题标题】:iOS NSCoding NSMutableArrayiOS NSCoding NSMutableArray
【发布时间】:2013-09-17 10:53:45
【问题描述】:

我有.h.m 来实现NSCoding,但mutableArray 对象计数始终为0...

.h

#import <Foundation/Foundation.h>

@interface Favorite : NSObject <NSCoding> {
    NSMutableArray *myArray;
}
@property (nonatomic, retain) NSMutableArray *myArray;
@end

.m

#import "Favorite.h"

@implementation Favorite
- (void)dealloc {
    [myArray release];
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:myArray];
}

- (id)initWithCoder:(NSCoder *)decoder {
    myArray = [[decoder decodeObject] retain];
    return self;
}
@end

我会像这样使用这个类

Favorite *fav = [[Favorite alloc] init];
fav.myArray = self.anotherArray;
[fav release];

对吗??

【问题讨论】:

    标签: ios objective-c nsmutablearray nscoding


    【解决方案1】:

    你需要像这样分配NSMutableArray

    myArray=[[NSMutableArray alloc]init];
    

    快乐编码

    【讨论】:

    • 我将init函数添加到Favorite.m仍然得到myArray count 0,请帮助我。 - (id)init { if (self = [super init]) { NSMutableArray *array = [[NSMutableArray alloc] init]; self.myArray = 数组; [数组释放]; } 返回自我; }
    • [编码器 encodeObject: myArray forKey:@"myArray"];然后在解码 myArray = [decoder decodeObjectForKey:@"myArray "];...试试这个
    • 非常感谢!!我写了一个示例,它的工作!我将检查我的代码以找出为什么数组计数始终为 0,再次感谢 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多