什么时候应该使用NSURL而不是NSString,反之亦然?

编程入门 行业动态 更新时间:2024-10-11 05:24:45
本文介绍了什么时候应该使用NSURL而不是NSString,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这不是有关问题的问题。

This is not a question about a pertinent problem. It's a question by which I try to deepen my understanding of Objective-C or more specific Cocoa Foundation.

在处理从服务器上传和下载文件到我的应用程序时,我总是在使用 NSURL 或 NSString 之间撕裂所有东西路径相关。当然,当有一个现有的API,我只是使用它根据规格。但是当我存储我自己的路径或创建处理他们的自定义类时,我困惑了哪两个是更好的选择。

When dealing with uploading and download files from a server to my apps, I'm constantly torn between using NSURL or NSString for all things path related. Of course when there's an existing API I just use it according to the specs. But when I store my own paths or create custom classes that deal with them, I'm confused which of the two would be the better pick.

NSString 无处不在,它有方便的方法,如 stringByAppendingPathComponent:和 stringByAppendingPathExtension:。我可以通过使用 [NSURL URLWithString:@string] 创建一个新实例,然后调用 [url path] ] 。但是区别是有原因的,对吧?

NSString is used everywhere and it has convenience methods like stringByAppendingPathComponent: and stringByAppendingPathExtension:. I can easily convert to NSURL by creating a new instance with [NSURL URLWithString:@"string"] and the other way around by calling [url path] on an NSURL instance. But the difference is there for a reason, right?

当我看看NSFileManager的头文件时,我的混乱增长。这两个方法非常接近:

My confusion grows when I look at header files of something like NSFileManager. These two methods are pretty close together:

- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error; - (BOOL)copyItemAtURL:(NSURL *)srcURL toURL:(NSURL *)dstURL error:(NSError **)error NS_AVAILABLE(10_6, 4_0);

为什么我会选择使用一个,特别是当两者之间的转换如此轻松?为什么苹果会遇到使用这两种数据类型创建近乎相同的API的麻烦?

Why would I choose to use one over the other, especially when conversions between the two are made so easily? And why does Apple go through the trouble of creating near-identical APIs for using both data types?

如果有人更深入地了解何时使用NSURL而不是NSString你自己的类处理文件路径和远程url,请分享!干杯。

If someone has the deeper insight for when to use NSURL instead of NSString for your own classes handling file paths and remote urls, please do share! Cheers.

推荐答案

一般来说,对于路径相关操作,您应该优先 NSURL over NSString ,因为路径信息可以更有效地存储在 NSURL 中(根据 NSFileManager )。所以我建议你的API也使用 NSURL 。

Generally for path related operations you should prefer NSURL over NSString because the path information can be stored more efficiently in NSURL (according to the class reference for NSFileManager). So I would recommend that for your APIs you use also NSURL.

也可以 NSURL 有 URLByAppendingPathComponent:和 URLByAppendingPathExtension:,因此方便: - )

Also NSURL has URLByAppendingPathComponent: and URLByAppendingPathExtension: so convenience is served as well :-)

更多推荐

什么时候应该使用NSURL而不是NSString,反之亦然?

本文发布于:2023-11-29 04:12:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:什么时候   而不是   NSURL   NSString

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!