Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 为什么NSURL类存在?为什么不使用URL字符串?_Objective C - Fatal编程技术网

Objective c 为什么NSURL类存在?为什么不使用URL字符串?

Objective c 为什么NSURL类存在?为什么不使用URL字符串?,objective-c,Objective C,我想知道为什么NSURL类存在。我认为下面的代码毫无意义 NSURL *url = [NSURL URLWithString:@"http://www.apple.co.jp/"]; NSError* error = [[NSError alloc] init]; NSString* content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; 为什么下面的代码是

我想知道为什么NSURL类存在。我认为下面的代码毫无意义

NSURL *url = [NSURL URLWithString:@"http://www.apple.co.jp/"];
NSError* error = [[NSError alloc] init];
NSString* content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
为什么下面的代码是不允许的

NSError* error = [[NSError alloc] init];
NSString* content = [NSString stringWithContentsOfURL:@"http://www.apple.co.jp/" encoding:NSUTF8StringEncoding error:&error];

感谢您的帮助。

因为面向对象编程。苹果觉得有足够的理由来支持一个表示URL的类,该类包含了比字符串表示更多的功能。文档始终是一个良好的起点:


可能是因为苹果觉得有足够的理由来支持一个代表URL的类,这个URL包含了比字符串表示更多的功能。查看文档:@Aaron你真的应该把它作为一个答案而不是评论。你可能是对的。事实上,如果能够说出类似NSString stringWithContentsOfURLString这样的话对您来说非常重要:@http://www.apple.co.jp/ 您可以通过一个类别将该方法添加到NSString中。感谢您的建议,这非常容易理解!