ios – 支持NSString连接多久?

我刚刚在编辑的一些旧代码中遇到了这一行:
[UIImage imageNamed:@"data/visuals/interface/" @"backgroundViewController"];
                                             ^^^^
                                   "Oops,what have I done here?"

我以为我必须意外地将错误的地方粘贴在一起,但是撤消并没有改变这一点.出于好奇,我建立了这个程序,并且成功了!

WhaddyakNow? Obj-c有一个更简洁的连接字符串文字方法.

增加了一些测试:

一个简单的日志

NSLog(@"data/visuals/interface/" @"backgroundViewController");

data/visuals/interface/backgroundViewController

参数

NSURL *url = [NSURL URLWithString:@"http://" @"test.com" @"/path"];
NSLog(@"URL:%@",url);

URL:http://test.com/path

使用变量

Nsstring *s = @"string1";
Nsstring *s2 = @"string2";

NSLog(@"%@",s s2);

Doesn’t compile (not surprised by this one)

其他文字

NSNumber *number = @1 @2;

Doesn’t compile

一些问题

这个字符串连接是否记录在任何地方?
>支持多久了?
>什么是底层实现?我希望这将是[s1 stringByAppendingString:s2]
>是否被任何权威机构认为是良好做法?

解决方法

这种连接静态Nsstrings的方法一个编译时间编译器,已经有十多年了.它通常用于允许长的常量字符串被分割成几行.几十年来,“C”中已经有类似的功能.

在C编程语言书中,1988第二版,第38页描述了字符串连接,所以它已经存在了很长时间.

本书摘录:

String constants can be concatenated at compile time:

"hello," " world" is equivalent to "hello,world"

This is useful for spitting long strings across several source lines.

Objective-C是“C”的严格超集,所以一直支持“C”字符串连接,我的猜测是因为静态的Nsstring连接一直是可用的.

当用于将静态字符串分割成多行以便可读性时,这被认为是很好的做法.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...