I want to check if the received data from a MySQL-database can be converted to Double and if that's possible I want to append it to an array.
From another question i got the following code:
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
}
}
Here's my concerned Code (which is in a for-loop):
if let value = datas[i].puls.doubleValue {
pulsWerte.append(value)
print(datas[i].puls)
}else {
badIndex.append(i)
continue
}
In case an entry of my database is NULL, my program should proceed in the else-branch. But if I test such a case I end up with a runtime-error and the following two messages:
-[NSNull length]: unrecognized selector sent to instance 0x1451238
&
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x1451238'
Does anyone know my mistake?
UPDATE
This is how my datas-array gets created:
For downloading and allocating the data I'm using Objectiv-C.
In my Data.h-file I create my Data-class.
@interface Data : NSObject
@property (nonatomic, strong) NSString *sozialversicherungsnummer;
@property (nonatomic, strong) NSString *messzeitpunkt;
@property (nonatomic, strong) NSString *puls;
@property (nonatomic, strong) NSString *sauerstoffgehalt;
@end
And in my HomeModel.m I create a NSMutableArray named "_data". After my download is finished this array gets filled as follows:
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];
}
When this step is completed I get this "_data"-array under the name of a parameter called "items" by a function called "itemsDownloaded".
And now I call this function in my Swift-file and here I'm able to typecast the downloaded "items" as "Data" (my self created class).
func itemsDownloaded(items: [AnyObject]!) {
let datas = items as! [Data]
.
.
.