Escaping the @“ ” characters in Objective C

冷暖自知 提交于 2019-12-06 19:49:39
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat:@"@%@",backgroundImageName]];

Essentially make two strings, it will add the @"" in the second.

You should use \ before the character you want. An example:

NSLog(@"\@\"Try\"");

The code prints

@"Try"

Don't forget that even string constants are NSString objects in Objective-C. This is part of the magic! I frequently see programmers new to the language writing this line:

[NSString stringWithFormat:@"@%@",backgroundImageName];

But you can simplify this line to:

[@"@" stringByAppendingString:backgroundImageName];

Magic!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!