我该如何退还NSJsonSerialization [英] How can I return NSJsonSerialization
问题描述
首先,我对Objective-C一无所知.
First, I know nothing about Objective-C.
这表示以下代码应从 AsyncStorage 获取数据.
That said the follow code should be getting the data from AsyncStorage.
我已经为 Android 做过类似的事情的基本思想是从AsyncStorage作为Json Object获取数据.
I've already did something like for Android where the basic idea is get the data from AsyncStorage as Json Object.
我现在需要的是在callInviteReceived
jsonFromLocalRNStrogeForKey 源
+(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];
}
那么,有没有人可以向我展示如何进行编码?
So, is there anyone who could show me, how to code this?
使用RCTAsyncLocalStorage + getAllKeys
推荐答案
代码看起来像这样(您需要提供storageKey
参数):
The code would look something like this (You will need to supply the storageKey
parameter):
- (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
是一个实用程序类,负责序列化和反序列化JSON.它的所有方法都是类方法,因此将其作为返回值传递毫无意义.
NSJSONSerialization
is a utility class responsible for serializing and deserializing JSON. All of its methods are class methods, so it's pointless to pass it as a return value.
反序列化的输出是NSDictionary
,它是JSON的Map表示.您可以使用此Map提取user.name
的值.
The output of deserialization is an NSDictionary
which is a Map representation of the JSON. You can use this Map to extract values of user.name
.
这篇关于我该如何退还NSJsonSerialization的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!