Failed in convert NSString from kABBirthdayProperty to NSDate - ios

I tried to convert NSString to NSDate as the instruction: NSString to NSdate
My snippet does not work, maybe the source NSString extracts incorrect
from ABPerson, with kABBirthdayProperty. It gets error with reason:
-[__NSTaggedDate bytes]: unrecognized selector sent to instance 0xc1c3a7619800000d
This is the snippet:
-(void) abRead: (ABPerson*) myRecord
{ if ([myRecord valueForProperty:kABBirthdayProperty] != nil)
{ NSDateFormatter* myBirthday = [[NSDateFormatter alloc] init];
[myBirthday setDateFormat:#"YYYY-DD-MM HH:MM:SS"];
NSDate* birthDay = [myBirthday dateFromString:[myRecord valueForProperty:kABBirthdayProperty]];
}
I guess that the NSString got from kABBirthdayProperty have some special format or some thing. I checked:
NSLog(#"birthday: %#", [myRecord valueForProperty:kABBirthdayProperty]);
it print out as NSString should be:
birthday :1980-02-08 05:00:00 +0000
Do I need any conversion from ABPerson:kABProperty to NSString correctly?
sorry for my bad English.

That property is already an NSDate*, it is not an NSString*. As such, you should not try and convert it from an NSString* to an NSDate* and just use it as an NSDate* directly:
-(void) abRead: (ABPerson*) myRecord {
NSDate* birthDay = [myRecord valueForProperty:kABBirthdayProperty];
if (birthday != nil) {
// Do something with birthday
}
}
The error you are getting actually tells you this:
-[__NSTaggedDate bytes]: unrecognized selector sent to instance 0xc1c3a7619800000d
This is telling you that an instance of type __NSTaggedDate is having the method bytes called on it. This implies that you're treating a type as if it is a different type.

Related

NSDate nil after returning from method

I have a simple method that determines the NSDate from a string like "NOV 2017". When I step through the method, actualDate has the correct value but nil is passed to the calling method.
The code below was working until recently, I'm on Xcode 9.0 targeting iOS 10 / 11.
Here is the calling method
NSDate *eventDate = [DateClass dateFromMonthYearString:#"NOV 2017"];
Method definition
+(NSDate*)dateFromMonthYearString:(NSString*)monthYearString
{
NSDateFormatter *formatter = [self cachedFormatter];
[formatter setDateFormat:MONTH_YEAR_DATE_FORMAT];
NSDate* actualDate = [formatter dateFromString:monthYearString];
return actualDate;
}
+(NSDateFormatter*) cachedFormatter
{
if (cachedDateFormatter == nil)
{
cachedDateFormatter = [[NSDateFormatter alloc] init];
cachedDateFormatter.locale = [NSLocale currentAppLocale];
}
return cachedDateFormatter;
}
The MONTH_YEAR_DATE_FORMAT is defined as ...
NSString * const MONTH_YEAR_DATE_FORMAT = #"MMM yyyy";
When I print the date object inside the method definition, Xcode prints the date correctly. When I try to 'po' outside the method definition, I just get something like this
(lldb) po eventDate
0x000000012eb048e0
Any thoughts ?
So when I print the values to the console via NSLog, it prints the correct value. Looks like its just the variable display and 'po' commands don't work correctly in X9.1 Weird ??

How to save only time zone without GMT?

I am using this code for getting time zone
NSMutableArray *arrResult = [NSMutableArray new];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
NSDate *myDate = [NSDate date];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setDateFormat:#"ZZZ"];
[[NSTimeZone knownTimeZoneNames] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:obj];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate: myDate];
NSMutableString *mu = [NSMutableString stringWithString:dateString];
[mu insertString:#":" atIndex:3];
NSString *strResult = [NSString stringWithFormat:#"(GMT%#)%#",mu,obj];
[arrResult addObject:strResult];
}];
NSLog(#"%#", arrResult);
the response of this code is like this as what i want
"(GMT+01:00)Africa/Libreville",
"(GMT+00:00)Africa/Lome",
"(GMT+01:00)Africa/Luanda",
"(GMT+02:00)Africa/Lubumbashi",
"(GMT+02:00)Africa/Lusaka",
"(GMT+01:00)Africa/Malabo",
"(GMT+02:00)Africa/Maputo",
"(GMT+02:00)Africa/Maseru",
"(GMT+02:00)Africa/Mbabane",
"(GMT+03:00)Africa/Mogadishu",
"(GMT+00:00)Africa/Monrovia",
"(GMT+03:00)Africa/Nairobi",
"(GMT+01:00)Africa/Ndjamena",
"(GMT+01:00)Africa/Niamey",
"(GMT+00:00)Africa/Nouakchott",
"(GMT+00:00)Africa/Ouagadougou",
"(GMT+01:00)Africa/Porto-Novo",
"(GMT+00:00)Africa/Sao_Tome",
"(GMT+02:00)Africa/Tripoli",
"(GMT+01:00)Africa/Tunis",
i have to show this type in my label
but what i have to do is when we click on save only (Africa/Tripoli) this part in the database will be saved , i am not getting how to do this .
please help me
If you don't want (GMT+01:00) this bracket in list then you can make string like,
NSString *strResult = [NSString stringWithFormat:#"%#",obj];
[arrResult addObject:strResult];
So, that bracket part will not come.
If you want that bracket part and name both together also for different task then you can make another array called arrResult2 and make another string called strResult2 and do as i mentioned above.
So, you have two arrays. one(arrResult) have both that bracket and names and one(arrResult2) have names only.
If you want to separate string then,
NSString *str = #"(GMT+00:00)Africa/Lome";
NSString *newStr = [str substringFromIndex:11];
NSLog(#"new str : %#",newStr);
I found that every string have closing bracket at 11th index, so you can use it to get sub string from original one.
Hope this will help :)
I got the answer after many try if anyone want this then see here:-
NSString *zoneString = #"(GMT+13:00)Pacific/Tongatapu"
NSUInteger location = [zoneString rangeOfString:#")"].location+1;
NSLog(#"Trimed string:%#",[zoneString substringFromIndex:location]);
Trimed string:Pacific/Tongatapu

EXC_BAD_ACCESS code=2 error on custom class initialization in Xcode

I'm attempting to initialize a custom class in my view controller to manage the data. Unfortunately the app crashes during the loading. I'm running Xcode 5.02 with the lldb debugger. The error I get is
Thread 1: EXC_BAD_ACCESS (code=2, address=0xbf7ffffc)
The error shows up on the first line (-(void)...) of the function
-(void)setDateOfErgPiece:(NSDate *)date
{
self.dateOfErgPiece = date;
if(self.dateOfErgPiece) {
// Date Formatter. So Date is displayed correctly
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
// Set value
self.dateOfErgPieceString = [NSString stringWithFormat:#"%#", [dateFormatter stringFromDate:self.dateOfErgPiece]];
}
}
That method is called when the object is initialized by
-(id)initWithDate:(NSDate *)date
{
self = [super init];
if(self) {
[self setDateOfErgPiece:date];
}
return self;
}
The (NSDate *)date value in the above init method is received from this method
-(ErgNewDataEntryLogic *)ergPieceData {
if(!_ergPieceData) _ergPieceData = [[ErgNewDataEntryLogic alloc] initWithDate:[NSDate date]];
return _ergPieceData;
}
What is causing this error? If you need more information I'd be happy to provide it. Thank you so much!
I don't know if this is exactly the problem or not, but this is certainly a major problem:
-(void)setDateOfErgPiece:(NSDate *)date {
self.dateOfErgPiece = date;
// ...
self.dateOfErgPiece = date; is exactly equivalent to [self setDateOfErgPiece:date];.
So, as the first line of the method, the method is calling itself. Infinite recursion.
This should be changed to the following...
-(void)setDateOfErgPiece:(NSDate *)date {
_dateOfErgPiece = date;
// ...
The other references to self.dateOfErgPiece within the method seem to be okay, because they look to be calling the getter:
[self dateOfErgPiece];

How do I fix this code so that it fetches only updated records?

I have an app that uses Ray Wenderlich SDSyncEngine & SDAFParseAPIClient, but Ive modified it to use my client's web service. When creating the fetch request, it uses a string to append to the GET that specified to only download records after updatedAt date. I log it here:
-[SDAFParseAPIClient GETRequestForAllRecordsOfClass:updatedAfterDate:] [Line 69] jsonString = {"updatedAt":{"$gte":{"__type":"Date","iso":"1900-01-01T00:00:00.999Z"}}}
-[SDAFParseAPIClient GETRequestForClass:parameters:] [Line 54] request <NSMutableURLRequest http://myserver.com/api_address_which_returns_XML_JSON?where=%7B%22updatedAt%22%3A%7B%22%24gte%22%3A%7B%22__type%22%3A%22Date%22%2C%22iso%22%3A%221900-01-01T00%3A00%3A00.999Z%22%7D%7D%7D>
That string is produced by this method:
- (NSMutableURLRequest *)GETRequestForAllRecordsOfClass:(NSString *)className updatedAfterDate:(NSDate *)updatedDate {
NSMutableURLRequest *request = nil;
NSDictionary *paramters = nil;
if (updatedDate) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.'999Z'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSString *jsonString = [NSString
stringWithFormat:#"{\"updatedAt\":{\"$gte\":{\"__type\":\"Date\",\"iso\":\"%#\"}}}",
[dateFormatter stringFromDate:updatedDate]];
NSLog(#"jsonString = %#", jsonString);
paramters = [NSDictionary dictionaryWithObject:jsonString forKey:#"where"];
}
request = [self GETRequestForClass:className parameters:paramters];
return request;
}
Which is originally from the tutorial. My webservice doesnt use date in iso format inside a dictionary. So I changed the code to:
NSString *jsonString = [NSString stringWithFormat:#"{\"updatedAt\":%#}",
[dateFormatter stringFromDate:updatedDate]];
and got this:
jsonString = {"updatedAt":1900-01-01T00:00:00.999Z}
but for some reason its not working, im still getting all the records from the web service each time.
I think the request you are posting on web server may be wrong. Please print the request string and then check it again.
The request you are sending having space that is the reason convert updatedAt : string to utf8encode and then check it again.
It may will help you.

Incompatible pointer types initializing 'NSDate

I am setting up some constants, one being an NSDate but receiving this wanring message:
Incompatible pointer types initializing NSDate *const __strong with an expression of type NSString
Simple explanation of code (imp file):
NSDate *const kPAPUserBirthdayKey = #"fbBirthday";
Advanced explanation:
I use a constants file as a singleton holding constant variables for the API i write to. For example the above which is a Date field which will hold the facebook users birthday when connecting to Facebook.
This is then later used in the following conversion:
// Convert the DOB string into Date format
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy"];
NSDate* userDOB = [df dateFromString:user.birthday];
[[PFUser currentUser] setObject:userDOB forKey:kPAPUserBirthdayKey];
Can someone explain what the warning actually means and what should be changed here? I get the same error on the last line of the above?
NSDate *const kPAPUserBirthdayKey = #"fbBirthday";
You are assigning a string to a NSDate.
Change NSDate to NSString.
Use:
NSString const *kPAPUserBirthdayKey = #"fbBirthday";
Also check what you need ?
A constant pointer or pointer to a constant.
NSDate *const kPAPUserBirthdayKey = #"fbBirthday";
Here fbBirthday is a string not date. The warning says that.
Change your constant's type to NSString. The compiler is telling you you're making an assignment between incompatible types, as NSString is not a subclass of NSDate.

Resources