When should I use NSURL instead of NSString and vice versa?

后端 未结 2 916
感动是毒
感动是毒 2020-12-16 20:22

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.

When d

相关标签:
2条回答
  • 2020-12-16 20:57

    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.

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

    0 讨论(0)
  • 2020-12-16 21:04

    NSUrl knows how to handle virtually any king of urls — not just Web-adresses and splits it into easy accessible chunks:

    • protocol or scheme (http, ftp, telnet,ssh)
    • username and the password (for example for ssh: ssh://user:password@host.domain.org)
    • host name
    • port
    • path
    • GET parameters

    Now you can easily asks the url-object for this chunks, while in a string there might be the need for excessive if rules or complicated regex's.

    0 讨论(0)
提交回复
热议问题