Swift 2.0 - “无法将'NSURL'类型的返回表达式转换为返回类型'String'”

时间:2015-11-14 20:51:53

标签: ios swift swift2

我正在尝试将旧的swift应用程序转换为2.0,并且似乎无法通过此函数中的这段代码:

func documentsPathForFileName(name: String) -> String {

    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true);

    let path = paths[0] ;

    let fullPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(name)

    return fullPath

}

在“let fullPath”行中,我收到错误“无法将类型'NSURL'的返回表达式转换为返回类型'String'”

这是完整的.swift文件:

import UIKit

class CardViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIScrollViewDelegate {

@IBOutlet weak var SecondCaptureButton: UIBarButtonItem!

@IBOutlet weak var imageView: UIImageView!

@IBOutlet weak var OpenButton: UIBarButtonItem!

@IBOutlet weak var MainCaptureButton: UIButton!

var imagePicker: UIImagePickerController!

 /* func supportedInterfaceOrientations() -> Int {
    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    } */


    override func shouldAutorotate() -> Bool {
        return false
    }

   override func viewDidLoad() {
    super.viewDidLoad()

    OpenButton.target = self.revealViewController()
    OpenButton.action = Selector("revealToggle:")

    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

    MainCaptureButton.layer.borderColor = UIColor.blackColor().CGColor
    MainCaptureButton.layer.cornerRadius = 10
    MainCaptureButton.layer.borderWidth = 1
    MainCaptureButton.titleLabel?.textAlignment = NSTextAlignment.Center

    let possibleOldImagePath = NSUserDefaults.standardUserDefaults().objectForKey("path") as! String?

    if let oldImagePath = possibleOldImagePath {

        let oldFullPath = self.documentsPathForFileName(oldImagePath)

        let oldImageData = NSData(contentsOfFile: oldFullPath)

        // here is your saved image:

        if let oldImage = UIImage(data: oldImageData!) {

            imageView.image = oldImage
            print("Old Photo Retrieved")

            self.view.bringSubviewToFront(imageView)

        }

    }
}

func documentsPathForFileName(name: String) -> String {

    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true);

    let path = paths[0] ;

    let fullPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(name)

    return fullPath

}

@IBAction func TakePhoto(sender: AnyObject) {
    imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .Camera

    presentViewController(imagePicker, animated: true, completion: nil)

}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imagePicker.dismissViewControllerAnimated(true, completion: nil)
    self.view.sendSubviewToBack(MainCaptureButton)
    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage

    let imageData = UIImageJPEGRepresentation(imageView.image!, 1)
    let relativePath = "image_\(NSDate.timeIntervalSinceReferenceDate()).jpg"
    let path = self.documentsPathForFileName(relativePath)

    imageData!.writeToFile(path, atomically: true)

    NSUserDefaults.standardUserDefaults().setObject(relativePath, forKey: "path")
    NSUserDefaults.standardUserDefaults().synchronize()

    print("New Photo Saved")


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}

2 个答案:

答案 0 :(得分:2)

您的方法被定义为返回String:

func documentsPathForFileName(name: String) -> String { ...

但您正在尝试返回NSURL。您可以更改方法签名以返回NSURL

func documentsPathForFileName(name: String) -> NSURL { ...

或者您可以返回URL的字符串表示形式:

return fullPath.absoluteString

答案 1 :(得分:1)

错误信息非常清楚。

NSURLString个实例,返回值为func documentsPathForFileName(name: String) -> NSURL { let fullPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(name) return fullPath }

更改返回值

func documentsPathForFileName(name: String) -> String {
    let fullPath = NSTemporaryDirectory().stringByAppendingPathComponent(name)
    return fullPath
}

或者返回一个字符串

path

我省略了局部变量i,因为它未在代码段中使用。