【问题标题】:NSDictionary and NSMutableDictionary combinationNSDictionary 和 NSMutableDictionary 组合
【发布时间】:2016-03-11 04:54:31
【问题描述】:

我的应用程序使用存储在 dataModel 类中的 deviceToken 类。设备令牌通过各种AFNetworking commands 在整个应用程序中使用。

//Notice the syntax for the deviceToken class in this NSDictionary
NSDictionary *params = @{@"cmd":@"join",                            
                         @"token":[_dataModel deviceToken],

下面是通过NSMutableDictionary 使用[_dataModel deviceToken] 类的另一种方式。

NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, @"command",                             
[_dataModel deviceToken], @"token", nil ];

问题是我的应用程序使用两种不同的 API,我想通过 j 子将 [_dataModel deviceToken] 类传递给新字典。此词典当前在 UILabel 中显示用户的用户名,称为 caption

//This message organizes photos, and shows the users username on them
-(id)initWithIndex:(int)i andData:(NSDictionary*)data
{
self = [super init];
if (self !=nil)
{
    //add the photo caption
    UILabel* caption = [[UILabel alloc] initWithFrame:CGRectMake(0, kThumbSide-32, kThumbSide, 32)];

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

我想用设备令牌格式化UILabel,即caption,如上所示,它当前显示用户名。问题是当我尝试像这样在标题标签中显示设备令牌时,我遇到了编译器问题。正确的语法是什么?

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

【问题讨论】:

  • 我想你忘了在 stringWithFormat caption.text = [NSString stringWithFormat:@"%@%@",[data objectForKey:@"token"],[_dataModel deviceToken]]; 中添加一个 % 符号

标签: ios json afnetworking


【解决方案1】:

检查你的stringWithFormat中的格式字符串:非常仔细,很明显编译器给了你一个正确且非常有用的警告。这整个问题与字典无关。

顺便说一句。使用最少的代码行没有价格。使用更多、更简单的语句使您的代码更容易调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    相关资源
    最近更新 更多