NSCoder编码器问题

时间:2015-12-11 00:55:40

标签: ios objective-c xcode nscoding

这个我的代码下面我想用这个文件进行单元测试。我想知道是否可以在XCTest中对此代码进行单元测试

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface Presenter : NSObject
@property (nonatomic, retain) NSString *Name;
@property (nonatomic, retain) NSString *Topic;
@property (nonatomic, retain) NSString *Date;
@property (nonatomic, retain) NSString *Time;
@property (nonatomic, retain) NSString *Location;
@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) NSMutableDictionary *rattings;
@property (nonatomic, assign) BOOL isLiked;



@end

这是M档案:

#import "Presenter.h"

@implementation Presenter

- (id) initWithCoder: (NSCoder *)coder
{
    if (self = [super init])
    {
        self.Name = [coder decodeObjectForKey:@"Name"];
        self.Topic = [coder decodeObjectForKey:@"Topic"];
        self.Date = [coder decodeObjectForKey:@"Date"];
        self.Time = [coder decodeObjectForKey:@"Time"];
        self.Location = [coder decodeObjectForKey:@"Location"];
        self.image = [coder decodeObjectForKey:@"image"];
        self.rattings = [coder decodeObjectForKey:@"rattings"];
        self.isLiked = [coder decodeBoolForKey:@"isLiked"];

    }

    return self;


}

- (void) encodeWithCoder: (NSCoder *)coder
{
    [coder encodeObject: self.Name forKey:@"Name"];
    [coder encodeObject: self.Topic forKey:@"Topic"];
    [coder encodeObject: self.Date forKey:@"Date"];
    [coder encodeObject: self.Time forKey:@"Time"];
    [coder encodeObject: self.Location forKey:@"Location"];
    [coder encodeObject: self.image forKey:@"image"];
    [coder encodeObject: self.rattings forKey:@"rattings"];
    [coder encodeBool: self.isLiked forKey:@"isLiked"];

}


@end

我发现这是关于NScoder单元测试的

  

How to unit test NSCoding?

并试图应用它,但我尝试它会发出错误: catch&#34; NSInvalidArgumentException&#34;,&#34; - [__ NSCFConstantString countByEnumeratingWithState:objects:count:]:无法识别的选择器

- (void)testName {
    NSLog(@"%@ start", self.name);   // self.name is the name of the test-case method.
    NSKeyedArchiver *archive = [[NSKeyedArchiver alloc] initForWritingWithMutableData:(NSMutableData *)presenter];
    [archive encodeObject:presenter forKey:@"Name"];
    for (NSString *Name in presenter.Name) {
        [archive encodeObject:[presenter valueForKey:Name] forKey:@"Name"];
    }
    XCTAssertTrue([[presenter Name] isEqualToString:@"Name"], "Name");
    NSLog(@"%@ end", self.name);
}

1 个答案:

答案 0 :(得分:0)

您应该从reading the documentation开始学习如何使用NSKeyedArchiverNSKeyedUnarchiver

以下是您的方法的样子:

- (void)testName
{
    NSLog(@"%@ start", self.name);   // self.name is the name of the test-case method.

    Presenter *presenter = [[Presenter alloc] init];
    presenter.Name = @"John";
    NSMutableData *presenterData = [NSMutableData data];

    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:presenterData];
    [archiver encodeObject:presenter forKey:@"presenter"];
    [archiver finishEncoding];

    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:presenterData];
    Presenter *decodedPresenter = [unarchiver decodeObjectForKey:@"presenter"];
    [unarchiver finishDecoding];

    XCTAssertTrue([decodedPresenter.Name isEqualToString:presenter.Name], "Name");

    NSLog(@"%@ end", self.name);
}

但是您可以使用工厂方法以更简单的方式执行此操作:

- (void)testName
{
    NSLog(@"%@ start", self.name);   // self.name is the name of the test-case method.

    Presenter *presenter = [[Presenter alloc] init];
    presenter.Name = @"John";

    NSData *presenterData = [NSKeyedArchiver archivedDataWithRootObject:presenter];
    Presenter *decodedPresenter = [NSKeyedUnarchiver unarchiveObjectWithData:presenterData];

    XCTAssertTrue([decodedPresenter.Name isEqualToString:presenter.Name], "Name");

    NSLog(@"%@ end", self.name);
}