I am comparing one date (say start_date) with current date (current_date_from_db). But the comparison is failing with either of the two errors:
if ([current_date_from_db compare:start_date]== NSOrderedAscending)
{
NSLog(#"Yes");
}
else {
NSLog(#"No");
};
And
while([ current_date_from_db earlierDate:start_date] && [current_date_from_db laterDate:end_date])
The code is failing with
SIGBART: Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[NSCFString isEqualToDate:]
current_date_from_db is a string as you can see in your error message: [NSCFString isEqualToDate:]. You'll want to parse your date and then compare:
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:current_date_from_db];
You need to do conversion prior to comparison. Either make an NSDate object with the current_date_from_db or convert the start_date to a string and then compare.
Related
So here's some code that I'm having trouble with:
//format the date to a string for echoing it
NSDateFormatter* formattedDate = [[NSDateFormatter alloc] init];
[formattedDate setDateStyle:NSDateFormatterLongStyle]; //now myFormatted is set to a long style
NSString* dateForOutput = [formattedDate stringFromDate:self.datePickerOutlet.date];
//also now need to set the "you began on" text to the newly chosen date
[self.startDate setText:#"You started on: %#", dateForOutput];
The error that is given is: "Too Many Arguments to method call, expected 1, have 2"
I don't see why it's saying that I'm trying to pass in two methods.
I tried to do the following in case I was being stupid but it still gave me an error:
//format the date to a string for echoing it
NSDateFormatter* formattedDate = [[NSDateFormatter alloc] init];
[formattedDate setDateStyle:NSDateFormatterLongStyle]; //now myFormatted is set to a long style
NSString* dateForOutput = [formattedDate stringFromDate:self.datePickerOutlet.date];
//also now need to set the "you began on" text to the newly chosen date
NSString *foobar = #"You started on: %#", dateForOutput;
[self.startDate setText:foobar];
Error given: "Interface type cannot be statically allocated"
Frankly I have no idea why it's giving me this error... some help would be greatly appreciated.
It's probably just something small that I'm just not seeing for some reason =/
cheers,
Matt
Instead of the line
[self.startDate setText:#"You started on: %#", dateForOutput];
in the first block of code you have given, try the following line
[self.startDate setText:[NSString stringWithFormat:#"You started on: %#", dateForOutput]];
But it is better to go with the second statements,
NSString *foobar = [NSString stringWithFormat:#"You started on: %#", dateForOutput];
you are doing things in wrong way
You should do the Things in this way
NSDate* myDate=[NSDate new];
NSDateFormatter* formattedDate = [[NSDateFormatter alloc] init];
[formattedDate setDateStyle:NSDateFormatterLongStyle];
//now myFormatted is set to a long style
// here i just passed the current date
NSString* dateForOutput = [formattedDate stringFromDate:myDate];
//also now need to set the "you began on" text to the newly chosen date
NSString *foobar = #"You started on:";
//Now you can append The String
//this is the way you can append the string ..
foobar= [foobar stringByAppendingFormat:#"%#",dateForOutput];
[self.startDate setText:foobar];
I'm trying to get the fireDate from an already set notification
Heres my code:
NSArray *notificationArray = [[NSArray alloc] initWithObjects:[[UIApplication sharedApplication] scheduledLocalNotifications], nil];
if ([notificationArray count] > 0) {
NSDate *now = [NSDate date];
UILocalNotification *locNotification = [[UILocalNotification alloc] init];
locNotification = [notificationArray objectAtIndex:0];
NSDate *otherDate = locNotification.fireDate;
}
The locNotification has the values but the last line when I try to instantiate otherDate I'm getting
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM fireDate]: unrecognized selector sent to instance 0x1e4e20'
* Call stack at first throw:
I really don't know what I'm doing wrong.
Thanks in advance
You're using the wrong init method. Instead of NSArray's -initWithObjects:, you want:
NSArray *notificationArray = [[ NSArray alloc ] initWithArray:
[[ UIApplication sharedApplication ]
scheduledLocalNotifications ]];
You get the exception because notificationArray only contains one object, the array returned by -scheduledLocalNotifications.
This is driving me nuts so I hope someone can help an Objective-C noob figure this out. Here's the deal:
I'm developing an iPhone app using Titanium Appcelerator and I'm trying to create an add-on module in XCode that will allow me to send an event to the iPhone calendar. What I'd like to do is to calculate the date and time in terms of seconds since Jan 1, 2001 (GMT) and send it directly to calendar without having to mess with the string-to-date stuff that seems always to return the wrong time. To this point, I've not been able to get the integer into the event date fields, both of which are NSDate types.
Titanium takes arguments from Javascript and compiles it into object code, so I can call my "cal" object like this:
var startDate = 316367923;
var endDate = 316367923;
var results = cal.newEvent(startTime,endTime)
. . . and this is how the "cal" object receives that call:
-(BOOL)newEvent:(id)args {
id startDate = [args objectAtIndex:0];
id endDate = [args objectAtIndex:1];
...
What I'm hoping to do get these integers into the event object:
EKEventStore *eventDB = [[EKEventStore alloc] init];
EKEvent *theEvent = [EKEvent eventWithEventStore:eventDB];
...
theEvent.startDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: (int) startDate];
theEvent.endDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: (int) endDate];
This compiles with no errors but causes my app to bomb nonetheless, so I'm figuring I've got something missing in my understanding. Any idea of where I'm going wrong?
Thanks,
Mark
Quite probably 'args' is an NSArray, hence startDate and endDate are objects, not literals. You probably want to do something like:
theEvent.startDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:[startDate intValue]];
If e.g. startDate is an NSNumber. Otherwise, check out the Titanium docs to find out the type of numbers passed in.
This may not be the cause of your crash, but the initWithTimeIntervalSinceReferenceDate NSDate method is expecting an NSTimeInterval (defined as typedef double NSTimeInterval) not an integer.
i.e.: Its method signature is:
- (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)seconds
I'd bet startDate and endDate are being converted to NSNumber objects instead of ints. Therefore, try the following lines:
theEvent.startDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: (int) [startDate intValue]];
theEvent.endDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: (int) [endDate intValue]];
I doubt that your crash has anything to do with it but, you might be leaking. The lines:
theEvent.startDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: (int) startDate];
theEvent.endDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: (int) endDate];
assign an object with retain count 1, if the setter for startDate and EndDate takes ownership of them (I don't know if java will do that or not so I might be wrong) you must store them in a local variable before assigning them to theEvent, so that you can call release afterwards.
so it would look like this:
NSDate *sDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:[startDate intValue]];
theEvent.startDate = sDate;
[sDate release];
could you please help me ?
I'm setting up an UILocalNotification and It crashes when I try to set its userInfo dictionary. fetchedObjects contains 88 objects.
Here is the code :
NSDictionary* myUserInfo = [NSDictionary dictionaryWithObject: fetchedObjects forKey: #"textbody"];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
// défining the interval
NSTimeInterval oneMinute = 60;
localNotif.timeZone = [NSTimeZone localTimeZone];
NSDate *fireDate = [[NSDate alloc]initWithTimeIntervalSinceNow:oneMinute];
localNotif.fireDate = fireDate;
localNotif.userInfo = myUserInfo; //this is the line that crashes the app
[fetchedObjects release];
and the console gives me this :
Property list invalid for format: 200
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unable to serialize userInfo: (null)'
Any idea ?
Actually, even if using an NSCoding-compliant object, UILocalNotification will throw an NSInvalidArgumentException upon calling setUserInfo. The UILocalNotification apparently keeps a more stringent interpretation of property-list types, in which only the stock objects specified in the Property List Programming Guide are allowed. You can get around this by using NSKeyedArchiver to serialize your custom NSCoding-compliant object to an NSData instance, which may be safely passed to UILocalNotification in the userInfo dictionary.
Sounds like there are objects in your userInfo dictionary that don't implement the NSCoding protocol. Everything in that dictionary has to be able to be written to 'disk', since your app may not be running when the notification fires. If there's something in there that can't be serialized, this is the result.
StringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
//Regex Out Artist Name
//NSString *regEx = ;
NSArray *iTunesAristName = [stringReply componentsMatchedByRegex: #"(?<=artistname\":\")([^<]+)(?=\")"];
if ([iTunesAristName isEqual:#""]) {
NSLog(#"Something has messed up");
//Regex Out Song Name
}else{
NSLog(iTunesAristName);
}
NSLog(iTunesAristName);
[stringReply release];
I just keep getting this error ?
2010-09-29 21:15:16.406 [2073:207] *** -[NSCFArray length]: unrecognized selector sent to instance 0x4b0b800
2010-09-29 21:15:16.406 [2073:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray length]: unrecognized selector sent to instance 0x4b0b800'
2010-09-29 21:15:16.407 [2073:207] Stack: (
please help its driving me crazy
The first argument to NSLog is supposed to be a format string. You're passing an NSArray. When the function tries to treat your array as a string, you get that error. Instead, use NSLog(#"%#", iTunesAristName);.
Chuck has answered your question, but I've noticed something else that is problematic.
NSArray is an array, not a string, so [iTunesArtistName isEqual:#""] will never return true, because they are different classes. Even if iTunesArtistName was a string, it should be compared using the isEqualToString: method, not isEqual:.
If you want to extract only the artist's name, you might be able to do this:
NSArray *matches = [stringReply componentsMatchedByRegex: #"(?<=artistname\":\")([^<]+)(?=\")"];
if ([matches count] == 0)
{
NSLog(#"Could not extract the artist name");
}
else
{
NSString *iTunesArtistName = [matches objectAtIndex:0];
NSLog(#"Artist name: %#", iTunesArtistName);
}
I see you're using RegexKitLite, make sure you import libicucore.dylib, i was getting the same error until i imported that library.