此代码在名为username的UILabel中显示了用户的caption

caption.text = [NSString stringWithFormat:@"@%@",[data objectForKey:@"username"]];

我希望caption代替显示该用户的device token而不是其username。到目前为止,这是device token在应用程序中成功使用的方式。
//A different Method that successfully handles the device token
 NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, @"command",                         [_dataModel deviceToken], @"token", nil ];


//Another different method that successfully handles the device token with a slightly different syntax
NSDictionary *params = @{@"cmd":@"join",
                         //the red text passes user_id to _datamodels userId method and thusly to api.php's handleJoin function
                         @"token":[_dataModel deviceToken],
                       };

问题:如何为objectforkey值编写正确的语法以显示 token ?这是我尝试过的方法,但收到编译器警告:no visible interface declaration for selector objectForKey。我知道这可以做到,怎么办?
caption.text = [NSString stringWithFormat:@"@%@",[data objectForKey:@"token":[_dataModel deviceToken]]];

原始儿子:数据
//Method body
-(id)initWithIndex:(int)i andData:(NSDictionary*)data
{
self = [super init];
if (self !=nil)
{

最佳答案

试试这个:

caption.text = [NSString stringWithFormat:@"%@", data[@"username"][@"token"]];

关于ios - NSString stringWithFormat objectForKey自定义Json数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35916406/

10-10 01:40