ios - NSNumber? to a value of type String? in CoreData -
so can't figure out, supposed change '.text' else or have go converting string double?
here code
if item != nil { // errors keep getting each 1 unitcost.text = item?.unitcost //cannot assign value 'nsnumber?' value of type 'string?' total.text = item?.total //cannot assign value 'nsnumber?' value of type 'string?' date.text = item?.date //cannot assign value 'nsdate?' value of type 'string?' }
you trying assign invalid type text
property. text property of type string?
stated compiler error. trying assign nsnumber
or nsdate
. expected type string
or nil
, must ensure provide only 2 possibilities. result, need convert numbers , dates strings.
in swift, there no need use format specifiers. instead, best practice use string interpolation simple types numbers:
unitcost.text = "\(item?.unitcost!)" total.text = "\(item?.total!)"
for dates, can use nsdateformatter
produce human-friendly date in desired format:
let formatter = nsdateformatter() formatter.datestyle = .mediumstyle date.text = "\(formatter.stringfromdate(date))"
while we're @ it, why not use optional binding instead of nil
comparison:
if let item = item { // set properties here }
Comments
Post a Comment