[英]Swift: Check if datatype can be converted to Double
我想檢查從MySQL數據庫接收到的數據是否可以轉換為Double,並且是否有可能將其附加到數組中。
從另一個問題,我得到以下代碼:
extension String {
struct NumberFormatter {
static let instance = NSNumberFormatter()
}
var doubleValue:Double? {
return NumberFormatter.instance.numberFromString(self)?.doubleValue
}
var integerValue:Int? {
return NumberFormatter.instance.numberFromString(self)?.integerValue
}
}
這是我關注的代碼(位於for循環中 ):
if let value = datas[i].puls.doubleValue {
pulsWerte.append(value)
print(datas[i].puls)
}else {
badIndex.append(i)
continue
}
如果我的數據庫條目為NULL ,則我的程序應在else -branch中繼續。 但是,如果我測試了這種情況,則會導致運行時錯誤和以下兩個消息:
-[NSNull length]: unrecognized selector sent to instance 0x1451238
&
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x1451238'
有人知道我的錯誤嗎?
這就是我的數據數組創建的方式:
為了下載和分配數據,我使用了Objectiv-C 。
在我的Data.h文件中,創建我的Data類。
@interface Data : NSObject
@property (nonatomic, strong) NSString *sozialversicherungsnummer;
@property (nonatomic, strong) NSString *messzeitpunkt;
@property (nonatomic, strong) NSString *puls;
@property (nonatomic, strong) NSString *sauerstoffgehalt;
@end
然后在我的HomeModel.m中創建一個名為“ _data”的NSMutableArray 。 下載完成后,該數組將如下填充:
for (int i = 0; i < jsonArray.count; i++)
{
NSDictionary *jsonElement = jsonArray[i];
// Create a new data object and set its props to JsonElement properties
Data *newData = [[Data alloc] init];
newData.sozialversicherungsnummer = jsonElement[@"Sozialversicherungsnummer"];
newData.messzeitpunkt = jsonElement[@"Messzeitpunkt"];
newData.puls = jsonElement[@"Puls"];
newData.sauerstoffgehalt = jsonElement[@"Sauerstoffgehalt"];
// Add this question to the locations array
[_data addObject:newData];
}
當這個步驟完成后,我收到了所謂的“項目”,由一個名為“itemsDownloaded”功能參數的名義下這個“_data” -陣列 。
現在,我在我的Swift文件中調用此函數,在這里我可以將下載的“項目” 轉換 為“數據” (我自己創建的類)。
func itemsDownloaded(items: [AnyObject]!) {
let datas = items as! [Data]
.
.
.
首先,您可以定義以下協議。
protocol DoubleConvertible {
func toDouble() -> Double?
}
接下來,對於要轉換為Double
每種類型,都需要使該類型符合DoubleConvertible
。
讓我們為String
做吧
extension String: DoubleConvertible {
func toDouble() -> Double? {
return Double(self)
}
}
和Int
extension Int: DoubleConvertible {
func toDouble() -> Double? {
return Double(self)
}
}
和...好Double
extension Double: DoubleConvertible {
func toDouble() -> Double? {
return self
}
}
現在給出幾種不同類型的值的列表
let list: [Any?] = ["123", nil, "Hello", 456, true, 789.1]
我們可以輕松地將其轉換為Double
列表。
let listOfDouble = list
.flatMap { $0 as? DoubleConvertible }
.flatMap { $0.toDouble() }
listOfDouble // [123.0, 456.0, 789.1]
您執行doubleValue:Double?
假設self是一個String
。
var doubleValue:Double? {
return NumberFormatter.instance.numberFromString(self)?.doubleValue
}
datas[i].puls
是由Objective-C JSON解析器創建的,在NULL
的情況下,該解析器會生成NSNull
的實例,但您希望將其設置為nil。
最簡單的方法是檢查jsonElement[@"Puls"]
為NSNull
然后設置newData.puls
以NULL
:
id puls = jsonElement[@"Puls"];
if puls == [NSNull null] {
newData.puls = null;
} else {
newData.puls = puls;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.