【问题标题】:Xcode retrieve userInfo from NSNotificationXcode 从 NSNotification 检索 userInfo
【发布时间】:2013-04-09 19:27:22
【问题描述】:

我有一个带有 userInfo 的 aNotification 我想用另一种格式调用另一种方法。 有没有办法从 [aNotification userInfo] 中检索字符串并修改它? userInfo 的格式如下: { action = "我想使用的字符串"; } 我确实喜欢这个(它几乎可以工作),但我觉得有更好的方法来做同样的事情。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playClicked:) name:kNotificationActionTapped object:nil];    

 ..................

-(void)playClicked:(NSNotification *)aNotification {
NSLog(@"Notification=%@", [aNotification userInfo]);

SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];

NSString *jsonString = [jsonWriter stringWithObject:[aNotification userInfo]];

[jsonWriter release];

NSString * string1 =  [jsonString substringFromIndex:11];
NSString * string2 = [string1 substringToIndex:[watch length] -2];


NSLog(@"string=%@", string1);
NSLog(@"string=%@", string2);

}

【问题讨论】:

    标签: xcode substring nsnotificationcenter


    【解决方案1】:

    userInfo 是一个 NSDictionary。使用标准的 NSDictionary 方法。

    NSString *aString = [aNotification.userInfo objectForKey:@"action"];
    

    【讨论】:

      猜你喜欢
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-26
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多