我怎样才能 return NSJsonSerialization
How can I return NSJsonSerialization
首先,我对Objective-C一无所知。
也就是说下面的代码应该从 AsyncStorage.
获取数据
我已经为 做了类似的事情,其中基本思想是从 AsyncStorage 获取数据作为 Json 对象。
我现在需要的是使用callInviteReceived
里面的jsonFromLocalRNStrogeForKey
jsonFromLocalRNStrogeForKey source
+(void)jsonFromLocalRNStrogeForKey:(NSString *)key completion:(void (^)(NSDictionary * _Nullable, NSError * _Nullable))completion
{
RCTResponseSenderBlock rnCompletion = ^(NSArray *response) {
NSString *jsonAsString;
if (response.count > 1) {
NSArray *response1 = response[1];
if (response1.count > 0) {
NSArray *response2 = response1[0];
if (response2.count > 1) {
jsonAsString = response2[1];
}
}
}
NSData *jsonAsData = [jsonAsString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *json = [
NSJSONSerialization
JSONObjectWithData:jsonAsData
options:NSJSONReadingMutableContainers
error:&error
];
completion(json, error);
};
RCTAsyncLocalStorage *storage = [RCTAsyncLocalStorage new];
dispatch_async(storage.methodQueue, ^{
[storage performSelector:@selector(multiGet:callback:) withObject:@[key] withObject:rnCompletion];
});
}
callInviteReceived
- (void)callInviteReceived:(TVOCallInvite *)callInvite {
NSJsonSerialization json = [self.jsonFromLocalRNStrogeForKey];
// json.user.name
[self reportIncomingCallFrom:@json.user.name withUUID:callInvite.uuid];
}
这些方法应该像
-(NSJsonSerialization)jsonFromLocalRNStrogeForKey: {
...
return json;
}
- (void)callInviteReceived {
...
NSJsonSerialization json = [self.jsonFromLocalRNStrogeForKey];
// json.user.name
[self reportIncomingCallFrom:json.user.name withUUID:callInvite.uuid];
}
那么,有没有人可以告诉我如何编写代码?
代码看起来像这样(您需要提供 storageKey
参数):
- (void)callInviteReceived:(TVOCallInvite *)callInvite {
[self jsonFromLocalRNStrogeForKey:/*storageKey*/ completion:^(NSDictionary* data,NSError* error){
if(data){
NSString * name = [data valueForKeyPath@"user.name"];
if(![name isKindOfClass:[NSNull class]]){
[self reportIncomingCallFrom:name withUUID:callInvite.uuid];
}
}else{
//handle error
NSLog(@"JSON Parsing Error: %@",error.localizedDescription)
}
}
}
NSJSONSerialization
是一个实用程序 class,负责序列化和反序列化 JSON。它的所有方法都是 class 方法,因此将它作为 return 值传递是没有意义的。
反序列化的输出是 NSDictionary
,它是 JSON 的 Map 表示。您可以使用此 Map 提取 user.name
.
的值
首先,我对Objective-C一无所知。
也就是说下面的代码应该从 AsyncStorage.
获取数据我已经为
我现在需要的是使用callInviteReceived
里面的jsonFromLocalRNStrogeForKey
jsonFromLocalRNStrogeForKey source
+(void)jsonFromLocalRNStrogeForKey:(NSString *)key completion:(void (^)(NSDictionary * _Nullable, NSError * _Nullable))completion
{
RCTResponseSenderBlock rnCompletion = ^(NSArray *response) {
NSString *jsonAsString;
if (response.count > 1) {
NSArray *response1 = response[1];
if (response1.count > 0) {
NSArray *response2 = response1[0];
if (response2.count > 1) {
jsonAsString = response2[1];
}
}
}
NSData *jsonAsData = [jsonAsString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *json = [
NSJSONSerialization
JSONObjectWithData:jsonAsData
options:NSJSONReadingMutableContainers
error:&error
];
completion(json, error);
};
RCTAsyncLocalStorage *storage = [RCTAsyncLocalStorage new];
dispatch_async(storage.methodQueue, ^{
[storage performSelector:@selector(multiGet:callback:) withObject:@[key] withObject:rnCompletion];
});
}
callInviteReceived
- (void)callInviteReceived:(TVOCallInvite *)callInvite {
NSJsonSerialization json = [self.jsonFromLocalRNStrogeForKey];
// json.user.name
[self reportIncomingCallFrom:@json.user.name withUUID:callInvite.uuid];
}
这些方法应该像
-(NSJsonSerialization)jsonFromLocalRNStrogeForKey: {
...
return json;
}
- (void)callInviteReceived {
...
NSJsonSerialization json = [self.jsonFromLocalRNStrogeForKey];
// json.user.name
[self reportIncomingCallFrom:json.user.name withUUID:callInvite.uuid];
}
那么,有没有人可以告诉我如何编写代码?
代码看起来像这样(您需要提供 storageKey
参数):
- (void)callInviteReceived:(TVOCallInvite *)callInvite {
[self jsonFromLocalRNStrogeForKey:/*storageKey*/ completion:^(NSDictionary* data,NSError* error){
if(data){
NSString * name = [data valueForKeyPath@"user.name"];
if(![name isKindOfClass:[NSNull class]]){
[self reportIncomingCallFrom:name withUUID:callInvite.uuid];
}
}else{
//handle error
NSLog(@"JSON Parsing Error: %@",error.localizedDescription)
}
}
}
NSJSONSerialization
是一个实用程序 class,负责序列化和反序列化 JSON。它的所有方法都是 class 方法,因此将它作为 return 值传递是没有意义的。
反序列化的输出是 NSDictionary
,它是 JSON 的 Map 表示。您可以使用此 Map 提取 user.name
.