I have an NSArray of Strings and what I want to do is create a new CalTask (for the calendar store) for every String, I want the Name of the task to be the string that is being added, the Priority and Due Date to be set in the code.
For example I have an array with the Strings To-Do 1, TD 2, TD 3.
So I want to create 3 CalTasks, the first one with the Name To-Do 1 and the second with the name TD 2 etc. See what I'm talking about. But I want all the Priorities and Due Dates to be the Same.
What you can do is loop over the NSArray and create and add a new CalTask with a predefined priority and due date:
// Set up the array
NSArray *array = [NSArray arrayWithObjects:#"TD1", #"TD2", #"TD3", nil];
// Get the calendar
CalCalendarStore *store = [CalCalendarStore defaultCalendarStore];
CalCalendar *calendar = [[store calendars] objectAtIndex:0];
// Note: you can change which calendar you're adding to by changing the index or by
// using CalCalendarStore's -calendarWithUID: method
// Define priority and due date
NSDate *dueDate = [NSDate date]; // By default due now - change as needed
CalPriority priority = CalPriorityMedium; // By default medium - change as needed
// Loop, adding tasks
for(NSString *title in array) {
// Create task
CalTask *task = [CalTask task];
task.dueDate = dueDate;
task.priority = priority;
task.title = title;
task.calendar = calendar;
// Save task
NSError *error = nil;
if(![store saveTask:task error:&error]) {
// Diagnostic error handling
NSAlert *anAlert = [NSAlert alertWithError:error];
[anAlert runModal];
}
}
Have a look at the Calendar Store Programming Guide.
Related
I am trying to get info on all the albums/photos using the PHPhotoLibrary. I barely know objective C, and i've looked at some tutorial/sample but couldn't find everything that I needed.
Here is a link to the sample code I based my code on.
https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html#//apple_ref/doc/uid/TP40014575-Intro-DontLinkElementID_2
So far I was able to get the albums name and identifier. And I am getting a list of photos, I am able to get their identifier as well, but not the filename. But if I put a break point in my fonction and look at my PHAsset pointer values, I can see the filename there (inside _filename), but if I try to call the variable with the filename in it, the variable does not exist.
So if anyone can provide a sample code to get all info on albums/photos/thumbnail that would be awesome. Or just getting the filename would be a good help.
Here is the code I have tried so far:
-(void)awakeFromNib{
NSMutableArray *allPhotos = self.getAllPhotos;
for (int x = 0; x < allPhotos.count; x ++)
{
PHAsset *photo = [self getPhotoAtIndex:x];
PHAssetSourceType source = photo.sourceType;
NSString *id = photo.localIdentifier;
NSString *description = photo.description;
NSUInteger height = photo.pixelHeight;
NSUInteger width = photo.pixelWidth;
NSLog(#"Test photo info");
}
}
-(PHAsset*) getPhotoAtIndex:(NSInteger) index
{
return [self.getAllPhotos objectAtIndex:index];
}
-(NSMutableArray *) getAllPhotos
{
NSMutableArray *photos = [[NSMutableArray alloc] init];
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"creationDate" ascending:YES]];
PHFetchResult *allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
PHFetchResult *fetchResult = #[allPhotos][0];
for (int x = 0; x < fetchResult.count; x ++) {
PHAsset *asset = fetchResult[x];
photos[x] = asset;
}
return photos;
}
As you can see, I can get the image height and width, its id, but cannot get the url to it.
I have found a way to get the url of my photo.
-(void)getImageURL:(PHAsset*) asset
{
PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];
[options setCanHandleAdjustmentData:^BOOL(PHAdjustmentData *adjustmentData) {
return [adjustmentData.formatIdentifier isEqualToString:AdjustmentFormatIdentifier] && [adjustmentData.formatVersion isEqualToString:#"1.0"];
}];
[asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info)
{
NSURL* url = contentEditingInput.fullSizeImageURL;
}];
}
Filenames in the Photos library are an implementation detail and subject to change. There are various private API for discovering them (or ways to use valueForKey or other public introspection APIs to find where they're hidden), they aren't something to be relied upon. In particular, an asset that's been edited is likely to have a different filename than the original.
What do you need a filename/URL for? If you're just uniquely identifying the asset across launches of your app, use localIdentifier. If you're showing it to the user... why? Something like IMG_0234.jpg vs IMG_5672.jpg has little meaning to the average user.
To fetch the assets in a specific album, use fetchAssetsInAssetCollection:options:. To fetch the album(s) containing a specific asset, use fetchAssetCollectionsContainingAsset:withType:options:. To discover the list(s) of albums, use other APIs on PHAssetCollection and its superclass PHCollection.
I have successfully downloaded and parsed (I think) the JSON data
NSURL *quoteURL = [NSURL URLWithString:#"http://www.qwoatzz.com"];
NSData *jsonData = [NSData dataWithContentsOfURL:quoteURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
However, I'm not too sure how to actually use the JSON data. If the JSON file has an array with two keys, how do I get a value from one of these keys at a specific index (the first one for example) and use that to change the text of a label (I know how to do that, it's just the JSON part I am stuck on)?
2014-10-20 19:46:10.616 Qwoatz-2[3147:454481] dataDictionary : {
count = 10;
"count_total" = 1871;
pages = 188;
posts = (
{
author = "Jason Seifer";
date = "2014-10-20 13:54:11";
id = 24317;
thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/10/1634685862_92b26b9167_o-150x150.jpg";
title = "What Employers Are Looking For in a Junior Rails Developer";
url = "http://blog.teamtreehouse.com/employers-looking-junior-rails-developer";
},
{
author = "Zac Gordon";
date = "2014-10-16 09:27:38";
id = 24296;
thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/10/brochure-site-150x150.png";
title = "When is WordPress.com the Right Solution?";
url = "http://blog.teamtreehouse.com/wordpress-com-right-solution-website";
},
{
author = "Gill Carson";
date = "2014-10-15 12:52:43";
id = 24287;
thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/10/Tahoe_team-Photo-150x150.jpg";
title = "We Are Family – The Whole Team!";
url = "http://blog.teamtreehouse.com/family";
},
{
author = "Jason Seifer";
date = "2014-10-14 15:26:11";
id = 24292;
thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/10/Chartist-Simple-responsive-charts-2014-10-14-15-24-43-150x150.jpg";
title = "Responsive Charts";
url = "http://blog.teamtreehouse.com/responsive-charts";
},
{
author = "Guil Hernandez";
date = "2014-10-13 09:28:05";
id = 24228;
thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/09/blend-mode-mult-150x150.jpg";
title = "Cutting-Edge CSS Features You Can Use Today";
url = "http://blog.teamtreehouse.com/cutting-edge-css-features-can-use-today";
},
{
author = "Faye Bridge";
date = "2014-10-10 09:00:45";
id = 24230;
thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/09/Nick-Bryan-150x150.jpg";
title = "After Just 6 Months Learning Nick is a full-time Web Developer";
url = "http://blog.teamtreehouse.com/6-months-nick-now-full-time-web-developer-major-computing-firm";
},
{
author = "Pasan Premaratne";
date = "2014-10-09 13:59:23";
id = 24250;
thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/10/Screen-Shot-2014-10-06-at-5.57.16-PM-150x150.png";
title = "Making a Network Request in Swift";
url = "http://blog.teamtreehouse.com/making-network-request-swift";
},
{
author = "Zac Gordon";
date = "2014-10-09 09:21:29";
id = 24278;
thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/10/wordpress_themes-150x150.jpg";
title = "New Course: WordPress Theme Development";
url = "http://blog.teamtreehouse.com/new-course-wordpress-theme-development";
},
{
author = "Dave McFarland";
date = "2014-10-08 13:47:55";
id = 24255;
thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/10/Screen-Shot-2014-10-06-at-1.02.40-PM-150x150.png";
title = "How to Install Node.js and NPM on a Mac";
url = "http://blog.teamtreehouse.com/install-node-js-npm-mac";
},
{
author = "Jason Seifer";
date = "2014-10-07 16:15:00";
id = 24273;
thumbnail = "http://blog.teamtreehouse.com/wp-content/uploads/2014/10/html5-device-mockups-150x150.jpg";
title = "Device Mockups";
url = "http://blog.teamtreehouse.com/device-mockups";
}
);
status = ok;
}
An example JSON file that was parsed.
Loop through the dictionary:
for(NSString *key in dataDictionary) {
id myObject = [dataDictionary objectForKey:key];
//do something with myObject
}
An NSDictionary is not an ordered collection, so there is no guarantee that looping through a dictionary as shown above will always loop through the keys in the same order. Apple doesn't provide an ordered dictionary with Cocoa/Cocoa Touch, and generally it is a bad idea to subclass NSDictionary or NSMutableDictionary as they are part of a class cluster.
Looking at the text from your example, posts is actually an array full of dictionaries. Assuming all the keys in your example are constant across the JSON files that you will be fetching, you could retrieve it using
NSArray *posts = [dataDictionary arrayForKey:#"posts"];
This array already appears to be ordered by date. You could then get the title for each post
for(int i = 0; i < [posts count]; i++) {
NSString *title = [((NSDictionary *)(posts[i])) objectForKey:#"title"];
//do something with title
}
1) Do you know the data is an array?
the JSON file has an array with two keys,...
value from one of these keys at a specific index...
This is somewhat of a mixed metaphor for me. When I have a JSON Array or NSArray, I tend to only think of indices (since that how arrays are ordered), and when I have JSON Objects or NSDictionaries, I tend to think of keys.
So, does the return value look like this:
[ "cat", 1, "a" ]
or does the data look like this:
{
"cat": {
"count": 1,
"tag": "a"
}
}
The first example is an Array with 3 elements; the second is an Object with 1 member that itself has 2 members.
2) If the data is correctly parsed as either an NSArray, or NSDictionary ...
Then you simply need to extract the data you want, with the accessors available on either container.
E.g.
NSArray *a = ...
[a firstObject];
[a objectAtIndex:0]; // same as above
NSDictionary *d = ...
d[#"memberName"];
[d objectForKey:#"memberName"]; // same as above
You'll want to actually save that data, or pass it to be processed, instead of just invoking the accessor.
UPDATE: based on the example data updated in the question.
One method is that you could extract the data both a bit manually, and iteratively.
NSDictionary *dataDictionary = ...
NSInteger count = [[dataDictionary objectForKey:#"count"] integerValue];
NSInteger countTotal = [[dataDictionary objectForKey:#"count_total"] integerValue];
NSInteger pagesCount = [[dataDictionary objectForKey:#"pages"] integerValue];
NSString *status = [dataDictionary objectForKey:#"status"];
NSArray *posts = [dataDictionary objectForKey:#"posts"];
for (NSDictionary *post in posts) {
for (NSString *key in post) {
NSLog(#"%#: %#", key, post[key]);
}
}
When you log dataDictionary, unquoted elements that are clearly strings are strings, elements in quotes are strings, integers and other numbers are likely usable numbers, but they may be strings (depends on return format), the date will be a string (and you can use NSDate and NSDateFormatter to pretty print it), status is just a string; for posts, the '(' and ')' wrap an array, and '{','}' wrap dictionaries.
UPDATE 2:
If you really want to do advanced searching, you can use NSPredicate to filter NSDictionary's or NSArray's. For example, something like the following would work:
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
#autoreleasepool {
NSDictionary *data = #{
#"stuff": #1,
#"posts": #[
#{ #"id": #1, #"title": #"one" },
#{ #"id": #2, #"title": #"two" },
#{ #"id": #3, #"title": #"three" },
#{ #"id": #4, #"title": #"four" },
#{ #"id": #5, #"title": #"five" },
]
};
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.id == %#", #4];
NSString *title = [[data[#"posts"] filteredArrayUsingPredicate:predicate] firstObject];
NSLog(#"title: %#", title);
}
}
prints
title: {
id = 4;
title = four;
}
You can use JSONModel Framework
JSONModel is a data model framework for iOS and OSX. It's written in Objective-C and helps you in several different ways.
You can read more about its key features below:
Rapidly write model code
Validation of the model's input
Atomic data
Type casting between JSON and Obj-C
Built-in data transformers
Custom data transformers
Model cascading
Convert back & forth from/to JSON
Persist model state in memory or file
Create models straight from the Internet
Automatic model compare methods
Note Please make sure your properties name match with key name in JSON
JSONModel Framework
GitHub Link
I have a problem with my algorithm for calculating a sore. The user enters a word into the UITextField, and if the word matches a string in the array (#"The Word") the int 'score' will be added by 1.
Then the int score is set as a label as the user gets a word right. (DISPLAYING THE SCORE)
THE PROBLEM, a user can just keep on entering the same word over and over again and the score will keep going up by one. IS there a command for knowing if a word has already been entered, so you can only use the word once.
The Code
NSArray *scoreArray1 = [NSArray arrayWithObjects:
#"Word 1", #"Word 2", #"Word 3", nil];
NSString *inputtwo =_EnterNameText.text;
BOOL isItright = NO;
for(NSString *possible in scoreArray1) {
if([inputtwo isEqual:possible] ) {
isItright = YES;
break;
}
}
if(isItright) {
static int myInt = 0;
myInt++;
NSString *score = [NSString stringWithFormat:#"%d", myInt];
[_scorelabel setText:score];
}
UPDATE!!!!!!
NSArray *scoreArray1 = [NSArray arrayWithObjects:
#"Alan Shearer", #"Shearer", #"Andrew Cole", #"Andy Cole", #"Cole", #"Thierry Henry", #"Henry", #"Robbie Fowler", #"Fowler", #"Frank Lampard", #"Lampard", #"Michael Owen", #"Owen", nil];
NSSet *set2 = [NSSet setWithArray:scoreArray1];
NSString *inputtwo =_EnterNameText.text;
BOOL isItright = NO;
for(NSString *possible in set2) {
if([inputtwo isEqual:possible] ) {
isItright = YES;
break;
}
}
if(isItright) {
static int myInt = 0;
myInt++;
NSString *score = [NSString stringWithFormat:#"%d", myInt];
[_scorelabel setText:score];
}
HOWEVER NOW THE APP DOES NOT WORK, IT CRASHES, any suggestions?
Why don't you keep a second Array where you store the given (correct) answers.
Whit this you can just do a contains inside your if....problem solved.
a second option is not to put string in your array but "Answer" Objects, that have a field that you can flag as already used.
You could just create an NSMutableSet and put a copy of the word into there whenever one is entered. Then you just need to check if the word exists in the set before incrementing the score.
I'm suggesting a set because it uses hashed access, so lookups are fast. Also, if you add the same string more than once, the set will still only have one reference to the string.
Actually, if you have an array of "legal" words, the way to go is to simply remove each word as it's called out, until the array gets to be zero entries long.
NSMutableArray* scoreArrayCopy = [NSMutableArray arrayWithArray:scoreArray];
int originalCount = scoreArrayCopy.count;
...
while (scoreArrayCopy.count > 0) {
NSString* guess = <get next guess>;
[scoreArrayCopy removeObject:guess];
score = originalCount - scoreArrayCopy.count;
}
(If you have a lot of words things would be more efficient if you used an NSMutableSet instead of an NSMutableArray, but the logic would be the same.)
I've set up a model with a few string fields and a few array fields. The model saves as such:
- (void)saveLevel:(NSString*)level traps:(NSArray*)traps whirls:(NSArray*)whirls accels:(NSArray*)accels walls:(NSArray*)walls dest:(NSString*)dest jupiter:(NSString*)jupiter rating:(NSNumber*)pRating;
{
if (m_pMOC == nil)
// Retrieves the managed object context
NSManagedObject* pNewLevel = [NSEntityDescription insertNewObjectForEntityForName:#"Level" inManagedObjectContext:m_pMOC];
NSDate* pDate = [NSDate date];
// Must have these four attributes
[pNewLevel setValue:level forKey:#"Level_ID"];
[pNewLevel setValue:jupiter forKey:#"Ball"];
[pNewLevel setValue:pDate forKey:#"Creation_Date"];
[pNewLevel setValue:dest forKey:#"Destination"];
[pNewLevel setValue:pRating forKey:#"Rating"];
// Optional attributes
if ([traps count] != 0)
[pNewLevel setValue:traps forKey:#"Traps"];
if ([whirls count] != 0)
[pNewLevel setValue:whirls forKey:#"Whirls"];
if ([accels count] != 0)
[pNewLevel setValue:accels forKey:#"Accelerators"];
if ([walls count] != 0)
[pNewLevel setValue:walls forKey:#"Walls"];
NSError* pError;
if (![m_pMOC save: &pError])
// etc...
}
I have a DataManager class which handles the fetching/saving from/to Core Data, and inside of the Manager I've verified that when I fetch an entity, the arrays are fetched, but when it's returned to the class that's calling it, the arrays are nil (the strings, on the other hand, arrive just fine). Here's the fetch code and the code that parses out the returned value from the fetch:
- (NSArray*) getLevelWithID:(NSString*)level
{
NSEntityDescription *pEntityDescription = [NSEntityDescription entityForName:#"Level" inManagedObjectContext:m_pMOC];
NSFetchRequest *pRequest = [[[NSFetchRequest alloc] init] autorelease];
[pRequest setEntity:pEntityDescription];
NSPredicate* pPredicate = [NSPredicate predicateWithFormat:#"Level_ID == %#", level];
[pRequest setPredicate: pPredicate];
NSError* pError;
NSArray* pLevels = [m_pMOC executeFetchRequest:pRequest error:&pError];
if (pLevels == nil)
{
NSLog(#"Could not find the level with Level_ID = %#",level);
abort();
}
NSArray* pAccels = [pLevels valueForKey:#"Accelerators"];
NSArray* pTraps = [pLevels valueForKey:#"Traps"];
NSArray* pWalls = [pLevels valueForKey:#"Walls"];
NSArray* pWhirls = [pLevels valueForKey:#"Whirls"];
return pLevels;
}
I set a break point on the last four Arrays, and they have objects in them, but in the function that retrieves them (shown below), they are nil.
- (void) initLevel:(NSArray*)pLevelObjects
{
Level* pLevel = [pLevelObjects objectAtIndex:0];
NSString* pJupiter = pLevel.Ball;
NSString* pDest = pLevel.Destination;
NSArray* pAccels = [pLevel valueForKey:#"Accelerators"];
NSArray* pTraps = [pLevel valueForKey:#"Traps"];
NSArray* pWalls = [pLevel valueForKey:#"Walls"];
NSArray* pWhirls = [pLevel valueForKey:#"Whirls"];
... (other initialization of the level) ...
}
I'm perplexed by this. The string values are there, but the arrays are not. I tried using the dot notation originally (NSArray* pAccels = pLevel.Accelerators, etc), but with the same result.
Ideas?
EDIT: initLevel is called from the view controller:
- (void) startGameWithLevelID:(NSString*)pLevelID
{
NSLog(#"MyViewController - beginGameWithLevelID");
NSArray* pLevel = [[DataManager getDataManager] getLevelWithID:pLevelID];
if (pLevel == nil || [pLevel count] == 0)
{
NSLog(#"Didn't retrieve any level with id %#", pLevelID);
abort();
}
else
{
CGRect newFrame = makeScreen([UIScreen mainScreen].applicationFrame);
GameBoard* pGB = [[GameBoard alloc] initWithFrame: newFrame];
pGB.m_pMyVC = self;
[pGB initLevel: pLevel];
[self.view addSubview: (UIView*)pGB];
[pGB release];
}
}
EDIT: I've newly re-factored the arrays into separate entities with a to-one relationship with the Level; the level has a to-many relationship with these entities. I also changed the names to better adhere to conventions: all attributes and relationships now start with a lowercased letter. I've prefixed the attributes and relationships with a_ and r_ respectively. I'm getting an error when saving "-[NSCFString _isKindOfEntity:]: unrecognized selector sent to instance 0x4d83120". It isn't terribly helpful but I'll let you know when I find the issue. For now, the code for saving looks as such:
- (void)saveLevel:(NSString*)level traps:(NSArray*)traps whirls:(NSArray*)whirls accels:(NSArray*)accels walls:(NSArray*)walls dest:(NSString*)dest jupiter:(NSString*)jupiter rating:(NSNumber*)pRating;
{
if (m_pMOC == nil)
{ // Code to get the managed object context from the delegate
}
NSManagedObject* pNewLevel = [NSEntityDescription insertNewObjectForEntityForName:#"Level" inManagedObjectContext:m_pMOC];
NSManagedObject* pNewBall = [NSEntityDescription insertNewObjectForEntityForName:#"Ball" inManagedObjectContext:m_pMOC];
[pNewBall setValue:jupiter forKey:#"a_Bounds"];
NSManagedObject* pNewDest = [NSEntityDescription insertNewObjectForEntityForName:#"Dest" inManagedObjectContext:m_pMOC];
[pNewDest setValue:dest forKey:#"a_Bounds"];
NSDate* pDate = [NSDate date];
// Must have these four attributes
[pNewLevel setValue:level forKey:#"a_Level_ID"];
[pNewLevel setValue:pDate forKey:#"a_Creation_Date"];
[pNewLevel setValue:pRating forKey:#"a_Rating"];
[pNewLevel setValue:#"Bob Dole" forKey:#"a_CreatedBy"];
[pNewLevel setValue:pNewBall forKey:#"r_Ball"];
[pNewLevel setValue:pNewDest forKey:#"r_Dest"];
// Optional attributes
if ([traps count] != 0)
[[pNewLevel mutableSetValueForKey: #"r_Trap"] addObjectsFromArray: traps];
if ([whirls count] != 0)
[[pNewLevel mutableSetValueForKey: #"r_Whirl"] addObjectsFromArray: whirls];
if ([accels count] != 0)
[[pNewLevel mutableSetValueForKey: #"r_Accel"] addObjectsFromArray: accels];
if ([walls count] != 0)
[[pNewLevel mutableSetValueForKey: #"r_Wall"] addObjectsFromArray: walls];
NSError* pError;
if (![m_pMOC save: &pError])
{ // Error saving
}
else
{ // Successfully saved
}
}
Again, thanks for the help, sorry for the lengthy post but I'm hoping this will help other Core Data newbies (like me) learn something.
FINAL EDIT: Ok, so after refactoring, I finally found a solution to save the arrays as objects (though whether it's the most efficient way I'm not sure). Below is just the code for adding a bunch of "Traps". The array passed into the function is the bounds of the Trap, which will be stored. The relationship is then formed with the Level (note it doesn't have to be set by both the Trap and the Level, just one of them, since Core Data takes care of the other way).
if ([traps count] != 0)
{
for (NSString* pBounds in traps)
{
NSManagedObject* pNewTrap = [NSEntityDescription insertNewObjectForEntityForName:#"Trap" inManagedObjectContext:m_pMOC];
[pNewTrap setValue:pBounds forKey:#"a_Bounds"];
[pNewTrap setValue:pNewLevel forKey:#"r_Level"];
}
}
Possible Array Misuse
In your first snippet, you are calling valueForKey: on an NSArray, and the result is an array, because that code iterates over pLevels, passes your valueForKey: argument to each object inside and combines the values.
It is how NSArray implements Key-Value Coding.
In your second piece of code, where you first fetch one pLevel from the array, you perform your valueForKey: selector on an entity, not on an array. So the result is what's in that particular object.
Probably some of your entities have those fields, so a combined result from all of those has some values, but some particular ones do not.
Possible Core Data Misuse
You say that you keep an array in your Core Data entity. How do you do that? There isn't a special type to hold an array, so normally it has to be stored as binary data with performing encoding using [NSKeyedArchiver archivedDataWithRootObject:array]. It might be that your storing and fetching is incorrect because you're trying to store arrays in non-array fields.
Do you also experience any crashes?
I have an events object which is given below
NSString *name;
NSString *date;
NSInteger id;
I am storing the events object in a NSMutabelArray. I want to add to the date and store in a different array. For that I am using the code below
NSString *curDate = event.Date;
NSDate *date = [dateFormat dateFromString:curDate];
for(int i=0;i<5;i++)
{
Events *newEvent = event;
NSDate *newDate = [date dateByAddingTimeInterval:60*60*24*1];
newEvent.date = [dateFormat stringFromDate:newDate];
[deleg.events addObject:newEvent];
date = newDate;
}
So after final iteration of loop all the objects in deleg.events is having the last calculated date. How can I resolve it.
Thanks
You're not making a new event. Your line
Events *newEvent = event;
is just creating a new variable that references the exact same event object, which means you now have added the exact same object to the array 5 times.
I don't know how your Events class works. If it conforms to NSCopying, then you can use
Events *newEvent = [[event copy] autorelease];
If not, you'll have to create a brand new Events object (using [[Events alloc] init] or whatever is appropriate for the class) and populate it with the appropriate data.