iOS:将URL解析为段 [英] iOS: parse a URL into segments

查看:87
本文介绍了iOS:将URL解析为段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取NSURL对象的有效方法如下:

What's an efficient way to take an NSURL object such as the following:

foo://name/12345 

并将其分解为一个字符串和一个无符号整数,其中字符串val是'name'和unsigned int是12345?

and break it up into one string and one unsigned integer, where the string val is 'name' and the unsigned int is 12345?

我假设算法涉及将NSURL转换为NSString,然后使用NSScanner的某些组件来完成剩下的工作?

I'm assuming the algorithm involves converting NSURL to an NSString and then using some components of NSScanner to finish the rest?

推荐答案

NSURL有一个方法 pathComponents ,它返回一个包含所有不同路径组件的数组。这应该可以帮助你获得整数部分。要获得名称,我将使用NSURL的主机方法。文档说,如果URL格式正确,它应该可以工作,那么也可以尝试一下。

NSURL has a method pathComponents, which returns an array with all the different path components. That should help you get the integer part. To get the name I'd use the host method of the NSURL. The docs say, that it should work if the URL is properly formatted, might as well give it a try then.

总而言之,不需要转换为字符串,似乎有很多方法可以从NSURL对象本身计算出URL的组成部分。

All in all, no need to convert into a string, there seems to be plenty of methods to work out the components of the URL from the NSURL object itself.

这篇关于iOS:将URL解析为段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆