iOS-与子类中的Optionals有关的Swift和Parse错误 [英] iOS - Swift and Parse error regarding Optionals within Subclass

查看:74
本文介绍了iOS-与子类中的Optionals有关的Swift和Parse错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个名为Filler.swift的Parse子类,该子类包含五个保存在Parse后端中的变量.

I have implemented a Parse subclass called Filler.swiftthat contains five variables that are held in the Parse backend.

尝试使用此子类将数据保存到Parse后端时,出现错误:fatal error: unexpectedly found nil while unwrapping an Optional value.

Whilst trying to save data to the Parse backend using this subclass I get the error: fatal error: unexpectedly found nil while unwrapping an Optional value.

这是导致错误的代码(在self.object.username =用户名行上):

This is the code that brings about the error (on the self.object.username = username line):

if let username = PFUser.currentUser()?.username {

        // set username equal to current user
        self.object.username = username

    }else{

        println("PFUser.currentUser()?.username contained a nil value.")

    }

我发现这与我在子类中处理可选变量的方式有关,但是Parse文档尚不清楚确切的操作方式.这是我的子类代码:

I've figured out that it's something to do with how I'm handling optional variables in my subclass but the Parse documentation isn't clear on exactly how to do this. Here's my code for the subclass:

class Fillup : PFObject, PFSubclassing {

var amount : String? {

    get {

        return self["amount"] as? String

    }

    set{

        self["amount"] = newValue

    }

}

var cost : String? {

    get {

        return self["cost"] as? String

    }

    set{

        self["cost"] = newValue

    }

}


var date : NSDate {

    get {

        return self["date"] as! NSDate

    }

    set{

        self["date"] = newValue

    }

}

var username: String? {

    get{

        return self["username"] as? String

    }

    set {


        self["username"] = newValue

    }
}


var id : Int?{

    get {

        return self["id"] as? Int

    }

}

override class func initialize() {

    var onceToken : dispatch_once_t = 0;

    dispatch_once(&onceToken) {

        self.registerSubclass()

    }

}

class func parseClassName() -> String {

    return "Fillup"
}

}

任何帮助将不胜感激.

Any help would be really appreciated.

推荐答案

您是否尝试添加?像这样: self.object?.username = username

Did you try adding a ? like so: self.object?.username = username

这篇关于iOS-与子类中的Optionals有关的Swift和Parse错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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