1

我有这个对象:

@interface EmailToSend : NSObject

@property (copy, nonatomic) NSString *Subject;
@property (copy, nonatomic) NSString *Body;
@property (strong, nonatomic) NSArray *Cc;
@property (strong, nonatomic) NSArray *Bcc;
@property (strong, nonatomic) NSArray *To;
@property (strong, nonatomic) EmailAddress *From;
@property (copy, nonatomic) NSString *Username;
@property (copy, nonatomic) NSString *Password;

@end

Bcc, To, From as Array EmailAdress

@interface EmailAddress : NSObject

@property (nonatomic, assign) int Id;
@property (copy, nonatomic) NSString *address;
@property (copy, nonatomic) NSString *displayName;

@end

我使用 iOS 中的 JSON 框架来解析对象EmailToSend

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:emailToSend options:NSJSONWritingPrettyPrinted error:&writeError];
 NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

当我运行项目时,出现一个错误:

Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'Invalid type in JSON write
(EmailToSend)

如何解决?

4

1 回答 1

3

EmailToSend是您在应用程序中使用的类类型。但是,Cocoa 中内置的 JSON 序列化程序只能处理简单的类型,例如etc。如果你想让它工作,你NSString NSArray必须让你的属性为一个字符串。From

正如 Wain 在他的评论中指出的那样,根元素也需要是NSArrayor NSDictionary

于 2013-07-16T09:56:34.657 回答