Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

macos - Converting PathForResource To String Returning nil Swift

When loading a local resource file in a mac app

let urlpath = NSBundle.mainBundle().pathForResource("myResource", ofType: "html");

Converting the resource path to a string for NSURL always returns nil

    println("resource path = (urlpath)") <---resource logged ok

    web.frameLoadDelegate = self;
    let requesturl = NSURL(string: urlpath!)

   println("URL String  = (requesturl)"),<---- returns nil

    let request = NSURLRequest(URL: requesturl!)
    web.mainFrame.loadRequest(request)

Which presumably is why im getting the error found nil while unwrapping an Optional value What am I missing here to correctly convert the pathForResource to a string?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
if let myFileUrl = NSBundle.mainBundle().URLForResource("yourFile", withExtension: "html"){
            // do whatever with your url
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...