1

サーバーに送信するjson文字列を作成したいのですが、その方法がわかりません。次のコードを使用していますが、例外が発生します。

キャッチされなかった例外によるアプリの終了'NSInvalidArgumentException'、理由:'JSON書き込みの無効なタイプ

NSError *error = nil;

self.drill.drillTitle = drillTitle.text;
self.drill.crewName = teamName.text;
self.drill.objectives = objectives.text;
self.drill.category = category;
self.drill.benchmarks = benchmarkArray;
self.drill.crewMembers = crewArray;
self.drill.currentUser = [Session retreiveSession];

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.drill.drillTitle, @"drillTitle", self.drill.crewName, @"crewName", self.drill.objectives, @"objectives", self.drill.benchmarks, @"benchmarks", self.drill.crewMembers, @"crewMembers", self.drill.currentUser, @"currentUser", nil, self.drill.category, @"category", nil];

        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"JSON output: %@", jsonString);

私のドリルクラスはここにあります:

#import <Foundation/Foundation.h>
#import "Category.h"
#import "Users.h"
#import "Benchmark.h"

@interface Drill : NSObject

@property (nonatomic, retain) NSString *drillId;
@property (nonatomic, retain) NSString *drillTitle;
@property (nonatomic, retain) NSString *crewName;
@property (nonatomic, retain) NSString *objectives;
@property (nonatomic, retain) Category *category;
@property (nonatomic, retain) NSMutableArray *benchmarks;
@property (nonatomic, retain) NSMutableArray *crewMembers;
@property (nonatomic, retain) Users *currentUser;
@property (nonatomic, retain) NSString *totalDrillTime;
@property (nonatomic, retain) NSString *timestamp;
@property (nonatomic, retain) NSString *error;

- (id)initWithCoder:(NSCoder *)coder;
- (void)encodeWithCoder:(NSCoder *)encoder;

@end

#import "Drill.h"

@implementation Drill

@synthesize drillId;
@synthesize drillTitle;
@synthesize crewName;
@synthesize objectives;
@synthesize category;
@synthesize benchmarks;
@synthesize crewMembers;
@synthesize currentUser;
@synthesize totalDrillTime;
@synthesize timestamp;
@synthesize error;


- (id)initWithCoder:(NSCoder *)coder {

    self = [super init];
    if(self) {
        drillId = [coder decodeObjectForKey:@"id"];
        drillTitle = [coder decodeObjectForKey:@"drillTitle"];
        crewName = [coder decodeObjectForKey:@"crewName"];
        objectives = [coder decodeObjectForKey:@"objectives"];
        category = [coder decodeObjectForKey:@"category"];
        benchmarks = [coder decodeObjectForKey:@"benchmarks"];
        crewMembers = [coder decodeObjectForKey:@"crewMembers"];
        currentUser = [coder decodeObjectForKey:@"currentUser"];
        totalDrillTime = [coder decodeObjectForKey:@"totalDrillTime"];
        timestamp = [coder decodeObjectForKey:@"timestamp"];
        error = [coder decodeObjectForKey:@"error"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {

    [encoder encodeObject:drillId forKey:@"id"];
    [encoder encodeObject:drillTitle forKey:@"drillTitle"];
    [encoder encodeObject:crewName forKey:@"crewName"];
    [encoder encodeObject:objectives forKey:@"objectives"];
    [encoder encodeObject:category forKey:@"category"];
    [encoder encodeObject:benchmarks forKey:@"benchmarks"];
    [encoder encodeObject:crewMembers forKey:@"crewMembers"];
    [encoder encodeObject:currentUser forKey:@"currentUser"];
    [encoder encodeObject:totalDrillTime forKey:@"totalDrillTime"];
    [encoder encodeObject:timestamp forKey:@"timestamp"];
    [encoder encodeObject:error forKey:@"error"];
}

@end
4

0 に答える 0