在Cocoa应用程序中验证用户输入的URL的最佳方法是什么? [英] What's the best way to validate a user-entered URL in a Cocoa application?
问题描述
我想建立一个自制的网页浏览器,以便更精通Cocoa。我需要一个好的方法来验证用户是否输入了有效的URL。我尝试了一些正则表达式,但NSString有一些有趣的怪癖,不喜欢一些反引号,我见过的大多数正则表达式使用。
您可以从 > http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURL/URLWithString: =nofollow noreferrer> NSURL
,如果字符串格式错误,则返回 nil
。
如果您需要进一步验证,可以使用 baseURL
, host
, parameterString
, path
等方法给你的URL的特定组件,然后你可以以任何你认为合适的方式评估。 p>
I am trying to build a homebrew web brower to get more proficient at Cocoa. I need a good way to validate whether the user has entered a valid URL. I have tried some regular expressions but NSString has some interesting quirks and doesn't like some of the back-quoting that most regular expressions I've seen use.
You could start with the + (id)URLWithString:(NSString *)URLString
method of NSURL
, which returns nil
if the string is malformed.
If you need further validation, you can use the baseURL
, host
, parameterString
, path
, etc methods to give you particular components of the URL, which you can then evaluate in whatever way you see fit.
这篇关于在Cocoa应用程序中验证用户输入的URL的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!