I have installed the GM version of iOS 7, successfully upgraded everything needed to update my app to iOS 7 but there is one thing i am unable to troubleshoot related to NSDate. It was working fine with iOS 6 but in iOS 7 its not working.
In one file named TheItem.m, i am doing the following:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(#"TIMEEEEEEEEEEEEEEE %#", currentString);
[self setPubTime:currentString];
}
Then in:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ItemsCell";
//ItemsViewCell Is class of UITableViewCell having the IBOutlet pubTimeLabel
ItemsViewCell *cell = (ItemsViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
TheItem *item = [[channel items] objectAtIndex:[indexPath row]];
cell.pubTimeLabel.text = [item pubTime];
// NSLog(#"PUB PUB %#", item.pubTime);
return cell;
}
The NSLogs are showing the date string but its not showing in the tableviewcell. It was working fine on the iOS 6 but eversince i have updated it to iOS 7, the label which is supposed to show the date string in the tableviewcell is showing nothing. Tried asking the same question in iOS Dev Forum and based on the answer i received, its not an iOS 7 update problem.
Can anyone point out what is wrong? Thanks in advance.
UPDATE: I have installed iOS 7 GM on my device, and then from the App Store i have installed one of my app that was submitted using the same project and boom that date label IS NOT getting displayed in it.
UPDATE: Here are the few things i have done to troubleshoot: Used NSLogs to see if at any point its nil which it isn't, changed the tableviewcell height, created a new label, creating a new outlet, removed the backgrounds and cell images, changed the font and color of the label, tried the project in both simulator and device BUT STILL no luck.
UPDATE: Ok based on the comments i have received, it looks like that i have some background or color issue or something like that. I have done some more troubleshooting and it has nothing to do with the color or background or any such thing. In the following code i have made some replacements:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
// [self setPubTime:currentString]; THIS LINE IS REPLACED WITH THE FOLLOWING
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:#"EEE, d MMM yyyy hh:mm:ss ZZZZ"];
NSDate *newDate = [df1 dateFromString:currentString];
NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
[df2 setDateFormat:#"EEE,d MMM yyyy"];
NSString *dateString = [df2 stringFromDate:newDate];
NSLog(#"DATE STRING %#", dateString);
[self setPubTime:dateString];
}
Now the same label is showing date that proves that there is no color or background issue with the label. The only issue why i am not using this updated code is that it is showing date in few cells and in few its not showing anything and retuning null.
It is failing at the hh and you have to use HH in your date formatter in order for it to work.
Just use the 24(HH) or 12(hh) hour notation:
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
df1.locale = locale = [[NSLocale alloc] initWithLocaleIdentifier:#"EN"];
[df1 setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZZ"];
NSDate *newDate = [df1 dateFromString:currentString];
NSLog(#"NEW DATE %#", newDate);
Hope it helps. Thanks!
I know that dequeueReusableCellWithIdentifier is supposed to create new prototype cells on it's own, but there may be some possibility that something that use to always work in iOS6 is now broken on iOS7. Try adding an alloc-init block in your method:
EDIT: Sorry didn't see all the comments, didn't realize you had the cell returning already.
Other than this you need to break in your cell returning code and make sure that the cell and the label are both non-nil. Did you try setting the label to #"Hello World" by chance?
Related
I have a number of Game objects, each one having a date parameter.
In my controller I want to fetch those games, grouped by the month-year combination of their dates - so I can get a list of months, and for each month - a list of the games that happened in that month.
Is there a simple solution for this, without having to modify my CoreData model?
Custom Section Titles with NSFetchedResultsController - this sample project shows how to do this (if I understood you correctly). Obviously, it doesn't use MagicalRecord, but it should be easy to port to MR.
this worked for me
in your
model.h
add
-(NSString *)yearMonth;
and
model.m
-(NSString *)yearMonth
{
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"yyy-MM"];
NSString *formattedDateString = [dateFormatter stringFromDate:self.eventDate];
return formattedDateString;
}
Then call a MR_Fetch by group method like this:
NSFetchedResultsController *frc = [model MR_fetchAllGroupedBy:#"yearMonth" withPredicate:nil sortedBy:#"eventDate" ascending:NO];
in an ios application, I am trying to allow the user to enter a number and a format and I want to get a string with that number formatted as the user specified:
For that I have tried this:
- (NSString *)formatNumber:(NSNumber *)number withFormat:(NSString *)format
{
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setPositiveFormat:format];
NSString *formatedString = [formatter stringFromNumber:number];
return formatedString;
}
but if I try to call something like this:
NSNumber *number = [NSNumber numberWithDouble:1234567890];
NSString *string = [self formatNumber:number withFormat:#"###-###-####"];
I was expecting to get a result: 123-456-7890
But it is not working.
is the #"###-###-####" format wrong syntax ? or am I using the NSNumberFormatter wrong?
Thanks for any help
PS: I do not want to hard code an algorithm to loop through the characters and insert the dashes (-) because that format is just an example as the user is able to freely change it.
Thanks
EDIT
SORRY while writting my code here I missed a line. now added it
This may be useful:
https://discussions.apple.com/thread/2399192
Key quote:
Parentheses and dashes are not allowed in a number format.
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 am trying to parse google weather API. I am using nsxml parser to get dates, weather etc from the api and then I store them in Core Data.
What I am trying to do is extract dates up from the parser, match them with the current date and then store only as many information we need to store.
Say, today's date is 08/09/2011 and a date from the parse matches. I wish to store only 2 information from the parser into Core Data. I am trying to store only those dates but I am getting all 4 information to be stored into Core Data.
If I give 08/11/2011, I should be only getting 3 days of information not 4. But I am unable to do that. I am posting my sample code. I am using testcase to check my application.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([#"forecast_date" isEqualToString:elementName])
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
startDate = [formatter dateFromString:[attributeDict objectForKey:#"data"]];
[formatter release];
}
else if ([#"forecast_conditions" isEqualToString:elementName])
{
isParsingForecast = YES;
isParsingInformation = NO;
isParsingCurrent = NO;
newdate=[startDate addTimeInterval:60*60*24*[forecastConditions count]];
NSMutableDictionary *fields = [[NSMutableDictionary alloc] init];
[fields setObject:newdate forKey:#"date"];
[fields setObject:city forKey:#"city"];
[fields setObject:state forKey:#"state"];
[fields setObject:country forKey:#"country"];
[fields setObject:startDate forKey:#"startdate"];
//[fields setObject: forKey:<#(id)#>]
[forecastConditions addObject:fields];
[fields release];
}
else if (isParsingForecast) {
NSMutableDictionary *fields = [forecastConditions lastObject];
NSLog(#"dic is : %# \n\n",fields);
[fields setObject:[attributeDict objectForKey:#"data"] forKey:elementName];
}
}
Posted my whole code here
http://www.iphonedevsdk.com/forum/iphone-sdk-development/87475-coredata-storing-more-values-than-what-required-store-database.html#post363100
The code at the link is unformatted and very hard to read. However, I did find one major problem.
This predicate will always fail:
predicate = [NSPredicate predicateWithFormat:
#"city == %# and state == %# and country == %# and date==%# and date==%#", city, state, country,startDate,endDate];
... if startDate and endDate values are not identical. You can't test the same key name against two different values and ever expect it to pass.
Since, a fetch returns only those objects who pass the predicate, a predicate that always fails always returns zero objects.
Since you apparently are using the predicate to find existing objects that already contain the parsed data, the always failing predicate means your code always thinks it needs to create a new managed object. That is why your object graph fills up with objects with duplicate values.
I am new to iPhone development. I am converting the date to the desired format and set it to the delegate and get its value in the another view. The session restarts when I tried to get the value from delegate. If I set the original date and not the formatted date in the set delegate, then I able to get the value in the another view. If I also give any static string value, then also I am able to the static string value back.
Only the formatted date which is string is set then the session restarts. If I print and check the value of the formatted date it prints the correct formatted date only. Here is my code for date conversion
NSString *dateval=[[stories objectAtIndex: storyIndex] objectForKey:#"date"];
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"EEE, MMM dd, yyyy"];
NSDate *inputDate = [inputFormatter dateFromString:dateval];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"MMMM dd"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];
AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
[delegate setCurrentDates:outputDate];
Edit
This is displayed in console
inside view did load
[Session started at 2010-04-21 19:12:53 +0530.]
GNU gdb 6.3.50-20050815 (Apple version gdb-967) (Tue Jul 14 02:11:58 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 4216.
(gdb)
In another view
- (void)viewDidLoad {
NSLog(#"inside view did load");
AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
NSString *titleValue=[delegate getCurrentDates];
self.navigationItem.title =titleValue ;
}
The get does not work properly. It works fine if I give any static string or the "dateval".
outputDate does not seems to be retained, so the value is lost at the end of the event loop (because of the NSAutoreleasePool).
You should retain the outputDate to avoid its release with something like that in the delegate:
- (void)setCurrentDates:(NSString *)value {
[value retain]; // <- Retain new value
[date release]; // <- Release old value;
date = value;
}
The best solution would be to have a declared property in the delegate with the retain attribute.