How to fetch date in iOS from string object - ios

I have stored NSDate in the DB using http://www.appcoda.com/sqlite-database-ios-app-tutorial/.
When i fetch the date back to NSString i get following during debugging. (As mentioned in tutorial even though i have a date picker and store value in NSDate, its actually stored as string in DB finally.
(lldb) po _itemPurchaseDate
(
)
Attached screenshot. So how do i fetch date and present it in a ui label in iOS?

Related

XCUITest, UIDatePicker, adjustToPickerWheelValue

I'm writing an XCUITest. I have a view with a UIDatePicker on it (NOT a regular picker).
I'm trying to set the date using adjustToPickerWheelValue.
This crashes with the following error:
caught "NSInvalidArgumentException", "-[XCUIElement(XCUIElementTypePickerWheel) adjustToPickerWheelValue:]_block_invoke can only be called with elements of type XCUIElementTypePickerWheel, not valid for DatePicker."
This implies of course that this method is not available for the UIDatePicker.
I've gone through the apple site and it states pretty clearly that it IS supported.
I can't find any examples of this used for a date picker.
It's possible of course that I'm just not setting it right (I'm not sure how one would pass a string date, for example).
This is the code I'm trying:
XCUIElement *DOB;
XCUIElementQuery *DOBList = app.datePickers;
DOB = [DOBList elementBoundByIndex:0];
NSLog(#"DOB = %#\n", [DOB description]);
[DOB adjustToPickerWheelValue:#"SOME DATE GOES HERE?"];
BTW - description is of a DatePicker.
Printing [DOB value] just returns an empty string.
Any ideas?
For using UIDatePickers, I do like that:
XCUIApplication().datePickers.firstMatch.pickerWheels["CurrentValue"].adjust(toPickerWheelValue: "NewValue")
You have to do that for each picker of the UIDatePicker.

IOS/Objective-C: Testing date for null very slow

I have some code that is running very slow. I narrowed down the issue to the following lines. If commented out, the code runs rapidly. If present, this process can take 30 seconds or more although no error is reported by the compiler or at runtime.
I have a date in a temporary object as follows:
NSDate *lastedited = importObject.lastedited;
If I log this to the console it logs as:
2015-10-08 08:44:51 +0000
The above does not cause a delay
When I then go to save it in Core Data, I first check if it is null. However, the following code is what causes the extreme delay:
if (lastedited != (id)[NSNull null]){
[record setValue:lastedited forKey:#"lastedited"];
}
Edit
I have discovered that even taking out the test, the line [record setValue:lastedited forKey:#"lastedited"] runs extremely slowly.
This is despite the fact that lastedited in the entity is an NSDate in the data model.
And as the line at the top shows it is in the form of an NSDate and logs to the console as 2015-10-08 08:44:51 +0000
What might be causing the code to run so slowly?
Is there a reason you're not simply testing against nil?
if (lastedited != nil) {
....
}
Here's a good overview of the different kinds of nothing in Objective-C/Cocoa: http://nshipster.com/nil/
I think the issue is with the data type of lastedited field in your Coredata entity. The data type you are using is NSDate, which is of course not a primitive data type supported by Coredata. If the data type is not one of the primitive types (like int, float, double, String, NSTimeInterval), Coredata will first convert it into one of the primitive types. In your case, the date is converted to NSTimeInterval just before saving, which is a bulky process and takes more time than expected. So if you are doing the overhead of converting the NSDate to NSTimeInterval before saving, the process will become faster. Try this
[record setValue:([lastedited timeIntervalSince1970] * 1000) forKey:#"lastedited"];
Don't forget to change the data type of lastedited to NSTimeInterval in .h file of your entity.
#property (nullable, nonatomic, copy) NSTimeInterval *lastedited;
Keep in mind that you are saving the date as milliseconds to coredata now. So whenever you are fetching it from coredata, convert it to NSDate and use

NSDate as function parameter in Swift

I'm facing an issue if I pass NSDate as a function parameter.
My code is:
self.PassDate(responseDate) ; // Response date value = 2015-05-15T00:00:00
func PassDate(date:NSDate) {
// Here ideally date value should be same as responseDate value.. but date is coming as 2015-05-14T07:00:00 PDT
}
Why passing NSDate is changing the timezone/values?
Nothing is changing. Different ways of viewing dates present them using different time zones. The underlying date is not changed. If you view the date in the debugger it (usually) shows it in UTC.
Try logging the date outside your function and inside using println() calls. You should see the same value in both places if you view it the same way each time.

iOS Insert Row with 2 Date Fields

How does one insert a row into a table that I've created with two sqlserver smalldatetime fields via the windows azure mobile services library? I'm currently trying to use:
[NSDate timeIntervalSince1970]
to store the date-time value as an NSTimeInterval or double. However, this causes the following error:
Error Domain=com.Microsoft.WindowsAzureMobileServices.ErrorDomain Code=-1302 "Error: Bad request."
DateTime fields should be represented by an NSDate field in the NSDictionary object you are sending to insert or update call.
So you would just do:
[table insert:#{ #"id": #"myid", #"deliveryDate": [NSDate date] } completion:... ];
If you send an NSNumber instead, then you would see the -1302 error shown above.

SUP error while sending date parameter to server

I am developing an iOS app using Sybase Unwired Platform 2.1.3 I am getting error while sending date parameter to database.
Here is my code,
#try
{
SUP105Sales_order *insertRow =[[SUP105Sales_order alloc]init];
int32_t idValue =2671;
int32_t custID =103;
int32_t sales =506;
insertRow.id_=idValue;
insertRow.cust_id=custID;
insertRow.order_date=[NSDate date];
insertRow.fin_code_id=#"r1";
insertRow.region=#"Hyd";
insertRow.sales_rep=sales;
[insertRow save];
[insertRow submitPending];
[SUP105SUP105DB synchronize];
}
#catch (NSException *exception) {
NSLog(#"Exception---%#",exception.description);
}
Server log,
2014-02-28 16:39:41.833 WARN Other Thread-182 [SUP105.server.SUP105DB]{"_op":"C","level":5,"code":412,"eisCode":"257","message":"com.sybase.jdbc3.jdbc.SybSQLException:SQL Anywhere Error -157: Cannot convert '' to a timestamp","component":"Sales_order","entityKey":"3210004","operation":"create","requestId":"3210005","timestamp":"2014-02-28 11:09:41.646","messageId":0,"_rc":0}
I think the error above is due to date format. I have tried few other ways to send date to the Sup server, but it did not work.
How to send DATE format to the sales_order table to the order_date column from iOS native app?
Appreciate your help.
The sybase doc says here
You cannot insert data into a timestamp column. You must either insert an explicit null by typing “NULL” in the column or use an implicit null by providing a column list that skips the timestamp column. Adaptive Server updates the timestamp value after each insert or update. See “Inserting data into specific columns” for more information.
In your case it seems that the value sent by your app is an empty string "" which is not allowed for a timestamp field. You can either skip the column in your insert request or set it to "NULL".
Could you try one those options.

Resources