我正在使用CoreData来存储数据。我正在使用NSNumber的数据类型存储ContactNumber。我正在从DB中检索数据
let entityDescription =
NSEntityDescription.entityForName("Retailer",
inManagedObjectContext: managedObjectContext)
let request = NSFetchRequest()
request.entity = entityDescription
do{
let results = try managedObjectContext.executeFetchRequest(request)
for test in results
{
print(test.valueForKey("address") as? String)
print(test.valueForKey("classes") as? String)
print(test.valueForKey("contactnumber") as! NSNumber)
print(test.valueForKey("landmark") as? String)
print(test.valueForKey("name") as? String)
print(test.valueForKey("outlettype") as? String)
print(test.valueForKey("retailername") as? String)
print(test.valueForKey("type") as? String)
print(test.valueForKey("credit") as? String)
self.arrayOfData.append((StoringClass(retailersname: (test.valueForKey("retailername") as? String)!, names: (test.valueForKey("name") as? String)!, outlettypes: (test.valueForKey("outlettype") as? String)!, types: (test.valueForKey("type") as? String)!, credits: (test.valueForKey("credit") as? String)!, landmarks: (test.valueForKey("landmark") as? String)!, classestype: (test.valueForKey("classes") as? String)!, addresses: (test.valueForKey("address") as? String)!, contactnumbers:s)))
}
但我的问题是我将ContactNumber视为“-27790.000000”。 我试图将NSNumber转换为String但它对我不起作用。
let s:String = String(format:"%f", test.valueForKey("contactnumber")!.doubleValue)
print(s) - output: “-27790.000000”
谁能告诉我我在做什么错误。
ThanksIn Advance
答案 0 :(得分:3)
请勿使用String(format:...)
。使用NSNumberFormatter
:
let f = NSNumberFormatter()
f.maximumFractionDigits = 0
let s = f.stringFromNumber(-27790.00)!
print(s) // Output "-27790"
答案 1 :(得分:0)
Swift 4.0 iOS 11.x
将NSNumber转换为字符串...
let temp:NSNumber = 5
let tempString = temp.stringValue