连接 Objective-C NSString
我是 Objective-C 的新手,所以请耐心等待。这就是我连接 URL 的方式:
id url = [NSURL URLWithString:@"http://blahblah.com/gradient.jpg"];
id image = [[NSImage alloc] initWithContentsOfURL:url];
id tiff = [image TIFFRepresentation];
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent: @"Desktop"];
NSString *fileToWrite = @"/test.tiff";
NSString *fullPath = [docsDir stringByAppendingString:fileToWrite];
[tiff writeToFile:fullPath atomically:YES];
它有效,但看起来很草率。这是连接 NSString 的理想方法吗?
I'm a total newbie at Objective-C, so bear with me. This is how I'm concatenating my URL:
id url = [NSURL URLWithString:@"http://blahblah.com/gradient.jpg"];
id image = [[NSImage alloc] initWithContentsOfURL:url];
id tiff = [image TIFFRepresentation];
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent: @"Desktop"];
NSString *fileToWrite = @"/test.tiff";
NSString *fullPath = [docsDir stringByAppendingString:fileToWrite];
[tiff writeToFile:fullPath atomically:YES];
It works, but it seems sloppy. Is this the ideal way of doing concatenating NSStrings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
stringByAppendingString:
或stringWithFormat:
几乎就是这样。stringByAppendingString:
orstringWithFormat:
pretty much is the way.您可以一次附加多个路径组件。例如:
您还可以在单个字符串中指定整个路径:
You can append multiple path components at once. E.g.:
You can also specify the entire path in a single string:
您是否研究过 NSMutableString< /a>?
Have you looked into NSMutableString ?
常见的约定是使用
[NSString stringWithFormat:...]
但它不执行路径追加 (stringByAppendingPathComponent)。A common convention is to use
[NSString stringWithFormat:...]
however it does not perform path appending (stringByAppendingPathComponent).