2015-09-02 148 views
0

我剛剛開始與迅速和核心數據。我有一個實體invoiceList並創建了一些屬性(一些字符串,整數32和十進制)。Swift與核心數據

我創建了一個編輯/添加視圖 - 控制,但現在我堅持附加的newitem部分。我收到錯誤「無法將字符串類型的值分配給類型爲NSNumber的值?」

我已經嘗試了不同的方式來投增值稅文本框爲INT但沒有成功。

我試圖

var invoiceVATString = nItem.valueForkey("invoiceVAT") as String 
    invoiceVATString = vat.text 

這裏的newitem FUNC

func newItem(){ 
    let context = self.context 
    let ent = NSEntityDescription.entityForName("invoiceList", inManagedObjectContext: context) 

    let nItem = InvoicesList(entity: ent!, insertIntoManagedObjectContext: context) 

    nItem.invoiceShopName = shopName.text 
    nItem.invoiceDescr = productDescription.text 
    nItem.invoiceVAT = vat.text<-cannot assign a value of type string? to a value of type NSNumber? 
    nItem.invoiceNumber = orderNumber.text<-cannot assign a value of type string? to a value of type NSNumber? 

} 

現在我知道invoiceVAT是一個核心型小數和還原數據屬性是一個文本框。我怎樣才能解決這個問題?

+0

檢索一個NSNumber的一個簡單的例子?一些增值稅號碼包含字母,順便說一句。 – Aderstedt

+0

對於invoiceVAT,我只需要數字和句點。 供參考:我想我已經命名它錯了,我的意思是bruto和netto的區別 – alex

回答

2

這裏有你想要`invoiceVAT`爲數值或字符串使用一個數字格式化從字符串

let aNumberString = "1235.45" 

let numberFormatter = NSNumberFormatter() 

if let theNumber = numberFormatter.numberFromString(aNumberString) { 

println(theNumber) 
} 
+0

錯誤消失了;)。這是在文本字段中顯示整數的方式,以及如何處理類型爲date的屬性?有沒有像NSDateFormatter存在? – alex

+0

有肯定的是,和字符串互操作使得將一個字符串一樣簡單「\ variableNameHere」 –

+0

FUNC的newitem內,我收到「致命錯誤:意外發現零而展開的可選值」我有註釋掉所有nItem域變量除了SHOPNAME但我仍然收到這條消息。你看到我要去哪裏嗎? – alex