开发者

Why my message become some monster char?

开发者 https://www.devze.com 2022-12-23 21:24 出处:网络
NSString *msg = [[NSString alloc]initWithFormat:@\"Hello %s, What do you do\", (self.isUser ? @\"User\" : @\"Guest\")];
    NSString *msg = [[NSString alloc]initWithFormat:@"Hello %s, What do you do", (self.isUser ? @"User" : @"Guest")];
    NSLog(msg);

Whe开发者_C百科n I print the msg, it show that this is "†¶·»,", instead of user/guest, what's happen?


You need to use "%@" instead of "%s" on your format string if you are using NSString instances. Like this:

NSString *msg = 
   [[NSString alloc]initWithFormat:@"Hello %@, What do you do", (self.isUser ? @"User" : @"Guest")];
0

精彩评论

暂无评论...
验证码 换一张
取 消