I've done a lot of object oriented programming in Java but im pretty new to objective C.
Am I doing the following properly.
Package.h
#interface Packages : NSObject
#property (nonatomic) NSArray *pictureArray;
#property (nonatomic) NSString *folderPath;
- (id)initWithPath:(NSString *)path;
-(NSArray *)getPackageItems;
-(NSString *)getFolderPath;
#end
Package.m
#implementation Packages
- (id)initWithPath:(NSString *)path
{
self = [super init];
if (self)
{
NSString *folderPath = [NSString stringWithFormat:#"%#/Objects/%#", [[NSBundle mainBundle] bundlePath], path]; //Path
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error: nil];
self.pictureArray = [fileList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"pathExtension IN %#", #"png", #"(content BEGINSWITH %#)", #"object_"]];
}
for (NSString *s in self.pictureArray) {
NSLog(#"%#", s);
}
return self;
}
-(NSArray *)getPackageItems{
return self.pictureArray;
}
-(NSString *)getFolderPath{
return self.folderPath;
}
#end
Code in question inside the viewdidload of one of my views
NSMutableArray *packages = [[NSMutableArray alloc] init];
for(id key in temp){
NSLog(#"key=%# value=%#", key, [temp objectForKey:key]);
Packages *tmp = [[Packages alloc] initWithPath:[temp objectForKey:key]];
[packages addObject:tmp];
}
so this for loop is iterating over a plist that I have. A series of paths.
for each path, it is creating an instance of Packages and adding it to an array of packages.
However, when do I do this
NSArray *something = [packages[1] getPackageItems];
I get an empty result back no matter what!
and even when I do this
NSLog(#"%#", [packages[0] getFolderPath]);
It prints out null.
What am I doing wrong.
EDIT
This is how the picture folder looks like
The code to pull it use to work when all of the pictures where in the Objects directory without any of the sub directories. I hope I edited the code properly to handle the sub dir
Edit 2
NSError *error;
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.folderPath error: &error];
NSLog(#"%#, %#", fileList , error);
on the line
NSString *folderPath = [NSString stringWithFormat:#"%#/Objects", [[NSBundle mainBundle] bundlePath], path]; //Path
which successfully prints
2014-03-23 21:25:32.200 ScrollBar[4475:60b] (
"object_eyes_0_0.png",
"object_eyes_10_1.png",
"object_eyes_10_2.png",
"object_eyes_10_3.png",
"object_eyes_10_4.png",
"object_eyes_10_5.png",
"object_eyes_10_6.png",
"object_eyes_10_7.png",
When I do
NSString *folderPath = [NSString stringWithFormat:#"%#/Objects/%#/objects", [[NSBundle mainBundle] bundlePath], path]; //Path
which outputs
2014-03-23 21:27:46.951 ScrollBar[4496:60b] (null), Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x8e2dda0 {NSUnderlyingError=0x8e2d020 "The operation couldn’t be completed. No such file or directory", NSFilePath=/Users/dev/Library/Application Support/iPhone Simulator/7.1/Applications/8B55D440-5727-471E-9BCC-1513C190740E/ScrollBar.app/Objects/object_eye_0_0/objects, NSUserStringVariant=(
Folder
)}
But as you can see in the image above, the file structure does exist.
Solution:
I accepted an answer even though it wasnt the real answer, it was the clear path to find out what was wrong.
The problem was that you can not have referenced folders containing sub folders using the code provided. I just many folders.
in your initWithPath:, you have NSString *folderPath = which declare a local variable and assign some value to it. However you did not save the value so self.folderPath is nil
- (id)initWithPath:(NSString *)path
{
self = [super init];
if (self)
{
self.folderPath = [NSString stringWithFormat:#"%#/Objects/%#", [[NSBundle mainBundle] bundlePath], path];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.folderPath error: nil];
self.pictureArray = [fileList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"pathExtension IN %#", #"png", #"(content BEGINSWITH %#)", #"object_"]];
}
for (NSString *s in self.pictureArray) {
NSLog(#"%#", s);
}
return self;
}
for logging error
NSError *error;
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.folderPath error: &error];
NSLog(#"%#, %#", fileList , error);
NEVER do NSLog([Someclass someMethod]), use NSLog(#"%#", [Someclass someMethod]) instead
Related
I am trying to edit existing objects in Core Data storage. When i edit each object's attribute without a loop this works fine, but when I try to do it in a for loop, only my last object gets new attributes.
Here is the code I use to edit objects without a loop. This code works fine):
NSFetchRequest *songsRefreshRequest = [[NSFetchRequest alloc] initWithEntityName:#"Music"];
songsRefreshRequest.predicate = nil;
songsRefreshRequest.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"name"
ascending:YES
selector:#selector(localizedStandardCompare:)]];
NSError *frError;
NSArray *fetchedSongs = [self.context executeFetchRequest:songsRefreshRequest error:&frError];
fetchedSongs ? : NSLog(#"Error in fetch while refreshing songs: %#", [frError localizedDescription]);
NSString *song1path = [[NSBundle mainBundle] pathForResource:#"Beautiful-birds-song-in-the-morning" ofType:#"mp3"];
NSString *song2path = [[NSBundle mainBundle] pathForResource:#"Birds-singing-relaxation" ofType:#"mp3"];
NSString *song3path = [[NSBundle mainBundle] pathForResource:#"Morning Melody" ofType:#"mp3"];
Music *song1 = (Music *) [fetchedSongs objectAtIndex:0];
Music *song2 = (Music *) [fetchedSongs objectAtIndex:1];
Music *song3 = (Music *) [fetchedSongs objectAtIndex:2];
song1.name = #"Beautiful-birds-song-in-the-morning";
song2.name = #"Birds-singing-relaxation";
song3.name = #"Morning Melody";
song1.path = song1path;
song2.path = song2path;
song3.path = song3path;
Here is the code I use to edit song's attributes in a loop (code below somehow edit a path for only the last object (song3 in my case), other object's path attribute becomes NULL. And I can't figure out why):
for (Music *iterSong in fetchedSongs){
NSString *iterSongName = [iterSong valueForKey:#"name"];
iterSong.path = [[NSBundle mainBundle] pathForResource: iterSongName ofType:#"mp3"];
}
I have already tried to change for each to for (int i=0; i<[fetchedSongs count]; i++) this didn't help. Cycle repeats as many times as many songs are in the model and only changes the last song's path. Appreciate any help, thanks in advance!
P.S. The whole loop code:
for (Music *iterSong in fetchedSongs){
//Music *iterSong = (Music*) [fetchedSongs objectAtIndex:i];
NSLog(#"inerSong.name = %#", iterSong.name);
NSLog(#"inerSong.oldPath = %#", iterSong.path);
NSString *iterSongName = [iterSong valueForKey:#"name"];
NSString *iterSongPath = [[NSBundle mainBundle] pathForResource: iterSongName ofType:#"mp3"];
NSLog(#"iterSongPath (string) = %#", iterSongPath);
iterSong.path = iterSongPath;
NSLog(#"iterSong.newPath = %#", iterSong.path);
}
if ([self.context hasChanges]){
NSError *error;
if (![self.context save:&error]){
NSLog(#"error while saving context after refreshing song paths: %#", [error localizedDescription]);
}
}
First, I assume you run that loop inside the NSManagedObjectContext performBlock method. Second, you need to save your changes to your NSManagedObjects context after you edit your objects.
Objective-C:
NSError *error = nil;
if ([[self managedObjectContext] save:&error] == NO) {
NSAssert(NO, #"Error saving context: %#\n%#", [error localizedDescription], [error userInfo]);
}
Swift:
do {
try managedObjectContext.save()
} catch {
fatalError("Failure to save context: \(error)")
}
Problem solved this way:
I removed all the hyphens from files' names. And the code started to work fine with the same loop. Also I converted files from mp3 to wav. But I suppose that problem existed because file can't include hyphen in its name.
Thanks everyone who answered!
EDIT: Have checked and rechecked file paths, code for archiving and unarchiving and no bugs found. Is there an issue with App File System between launches that I am not aware of?
I am using NSCoder to store images in my app. I create a unique file path, store that file path to Core Data, and archive an image using that same file path in the documents.
On the app's first launch, everything works as expected - the file paths saved in Core Data are used to unarchive the images stored in Documents. On subsequent launches, however, the unarchiver returns nil, and I get a crash.
ImageForArchiving.m - the object that is to be saved with init and encode methods
+ (ImageForArchiving *)createImageWithImage:(NSData*)data andDate:(NSDate*)date {
ImageForArchiving *archiveImage = [[ImageForArchiving alloc] init];
[archiveImage setDate:date];
[archiveImage setImageData:data];
return archiveImage;
}
- (id) initWithCoder: (NSCoder *)coder
{
if (self = [super init])
{
[self setDate: [coder decodeObjectForKey:#"date"]];
[self setImageData: [coder decodeObjectForKey:#"image"]];
}
return self;
}
- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject:self.date forKey:#"date"];
[coder encodeObject:self.imageData forKey:#"image"];
}
Saving to archive, Core Data, creation of file path and unarchiving code
- (void)saveItemsAtFilePath:(NSString*)filePath andImageToArchive:(ImageForArchiving*)imageRecord {
[NSKeyedArchiver archiveRootObject:imageRecord toFile:filePath];
}
- (void)loadItemsWithFilePath:(NSString*)filePath {
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
ImageForArchiving *image = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(#"image %#", image.date);
} else {
NSLog(#"nothing saved");
}
}
-(void)saveToCoreDataWithFilePath:(NSString*)filePath{
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = delegate.managedObjectContext;
NSManagedObject *photoAndDate = [NSEntityDescription insertNewObjectForEntityForName:#"Image" inManagedObjectContext:context];
[photoAndDate setValue:[NSDate date] forKey:#"date"];
[photoAndDate setValue:filePath forKey:#"image"];
NSError *error = nil;
if (![context save:&error]) {
NSLog(#"Can't Save! %# %#", error, [error localizedDescription]);
}
}
- (NSString *)createPathForDataFile
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
documentsPath = [documentsPath stringByExpandingTildeInPath];
NSError *error = nil;
if ([fileManager fileExistsAtPath: documentsPath] == NO)
{
[fileManager createDirectoryAtPath:documentsPath withIntermediateDirectories:YES attributes:nil error:&error];
}
NSString *stringForSaving = [[NSUUID UUID]UUIDString];
NSString *filePath = [documentsPath stringByAppendingPathComponent:stringForSaving];
return filePath;
}
Looping through Core Data array to load up data source. The addition of the unarchived object is w
I was encountered the same problem before. You write a image to a path and save it to the core data. Later read the image path, it is exactly same but no image is loaded.
So, Did you tested the app in the simulator? I don't know if it is intended or a kind of a bug but it would be working as you wanted on the real devices.
So try to run your app in the real devices and see if it works.
I just found out that in iOS 8 the file paths are reset after each app launch. The code above save the file name to Core Data and re-create the file path to Documents when calling for the file.
For instance, the below would create the file name.
- (NSString *)createPathForDataFile
{
NSString *stringForSaving = [[NSUUID UUID]UUIDString];
NSLog(#"filePath in CreatePathForDataFile %#", stringForSaving);
return stringForSaving;
}
Saving would occur by calling the path to the Documents folder and passing in the file name as the parameter filePath.
- (void)saveItemsAtFilePath:(NSString*)filePath andImageToArchive:(ImageForArchiving*)imageRecord {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *finalFilePath = [documentsPath stringByAppendingPathComponent:filePath];
[NSKeyedArchiver archiveRootObject:imageRecord toFile:finalFilePath];
}
I'm trying to read and write data to my plist file, While the reading part goes well, the writing part does nothing.
I might be mistaken for the writing part - i can't see any changes in my file under my bundle - it is still empty after my changes, and when i close the app and open it again - i still see empty email address line.
The code for writing (and placing the plist in the document folder for future writings)
NSFileManager *fileManger=[NSFileManager defaultManager];
NSError *error;
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];
NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:#"userData.plist"];
NSLog(#"plist path %#",destinationPath);
if ([fileManger fileExistsAtPath:destinationPath]){
NSLog(#"database localtion %#",destinationPath);
//return;
}
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:#"userData.plist"];
[fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: sourcePath];
NSString *emailAddress = (NSString *)[dict objectForKey: #"emailAddress"];
if([emailAddress isEqualToString:#""])
{
// Do stuff
}
And for writing
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doumentDirectoryPath =[pathsArray objectAtIndex:0];
NSString *destinationPath = [doumentDirectoryPath stringByAppendingPathComponent:#"userData.plist"];
NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile: destinationPath];
[plistDict setValue:#"myEmail#gmail.com" forKey:#"emailAddress"];
[plistDict writeToFile:destinationPath atomically: YES];
But as i said, nothing is changed in the file itself, and not even when i save, close the app and open it again (the string is always empty on my reading part)
Any help will be more than welcomed.
I'm trying to learn objective-c (I'm very new to that) and I have issues with memory management...
I'm developing an iPad app that uses TouchXML.
I've created my class that extends CXMLDocument and does some initialisation by reading some contents and saving into properties.
Here is my code (SimpleManifest.h):
#interface SimpleManifest : CXMLDocument {
CXMLNode *_defaultOrganization;
NSString *_title;
NSDictionary *dictionary;
}
#property (readonly) CXMLNode *defaultOrganization;
#property (readonly) NSString* title;
- (id) initWithPath:(NSString *)path options:(NSUInteger)options error:(NSError **)error;
#end
(SimpleManifest.m):
#import "SimpleManifest.h"
#import "CXMLNode_XPathExtensions.h"
#implementation SimpleManifest
- (id) initWithPath:(NSString *)path options:(NSUInteger)options error:(NSError **)error
{
/*
NSURL *theURL = [[[NSURL alloc] initFileURLWithPath:path] autorelease];
self = [self initWithContentsOfURL:theURL options:options error:error];
*/
NSData *data = [NSData dataWithContentsOfFile:path];
NSString *s = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
self = [self initWithXMLString:s options:options error:error];
if (self==nil) return nil;
// load main props
dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
#"http://www.imsglobal.org/xsd/imscp_v1p1", #"imscp",
#"http://ltsc.ieee.org/xsd/LOM", #"lom", nil];
// defualt organization
#try {
CXMLNode *orgsElem = [[[self childAtIndex:0] nodesForXPath:#"//imscp:organizations" namespaceMappings:dictionary error:nil] objectAtIndex:0];
NSString *xpath = [NSString stringWithFormat:#"//imscp:organization[#identifier='%#']", [[orgsElem attributeForName:#"default"] stringValue]];
_defaultOrganization = [[[self childAtIndex:0] nodesForXPath:xpath namespaceMappings:dictionary error:nil] objectAtIndex:0];
/*
NSArray *nodes = [[self childAtIndex:0] nodesForXPath:#"//imscp:organizations" namespaceMappings:dictionary error:nil];
NSString *xpath = [NSString stringWithFormat:#"//imscp:organization[#identifier='%#']", [[[nodes objectAtIndex:0] attributeForName:#"default"] stringValue]];
_defaultOrganization = [[[self childAtIndex:0] nodesForXPath:xpath namespaceMappings:dictionary error:nil] objectAtIndex:0];
*/
CXMLNode *titleElem = [[[self childAtIndex:0]
nodesForXPath:#"//lom:general/lom:title/lom:string"
namespaceMappings:dictionary
error:nil] objectAtIndex:0];
_title = [[titleElem stringValue] copy];
} #catch (NSException * e){
self = nil;
return nil;
}
return self;
}
#end
Later on in another class I do:
- (BOOL) isValidSCORMLesson:(NSString*) path {
NSString *manifPath = [path stringByAppendingPathComponent:#"imsmanifest.xml"];
if (![[NSFileManager defaultManager] fileExistsAtPath: manifPath isDirectory: NO])
return NO;
SimpleManifest *manifest = [[[SimpleManifest alloc] initWithPath:manifPath options:0 error:nil] autorelease];
NSLog(#"%#", manifest.defaultOrganization);
NSLog(#"%#", manifest.title);
return (manifest!=nil);
}
It gives me tons of "pointer being freed was not allocated" errors...
The thing changes if I comment out the NSLog calls above or just log the manifest.title property.
Project is not using ARC, so I'm sure I'm doing something wrong with memory management.
Can someone please help me understand where I'm doing wrong? Thanks!
There isn't anything obviously wrong with that code that would cause malloc errors. Best guess is that there is a bug in the CXMLDocument class/library or some mistake in the way you are using it.
Note that a "pointer being freed was not allocated" means that someone called free() (or dealloc, effectively) on a pointer to a piece of memory that was not allocated in the first place. It usually gives you a breakpoint you can set that will then give you a backtrace of exactly where it happened.
Some comments:
(1) Do not #try/#catch in that fashion. Just don't catch at all. The pattern you are using will hide any errors. Exceptions are not meant to be recoverable in iOS/Cocoa.
(2) You can create an NSString instance directly from a file; no need to load via NSData first.
(3) You should use ARC.
I am using this code to get book names from a config.plist file. However my memory management is problematic. The '[dict release]' breaks the app completely and it exits.
The code works when the '[dict release]' is removed but it causes memory leaks as far as I can tell.
bnames is a global NSMutableArray
What am I doing wrong?
- (NSString *)loadBookname: (NSInteger) bookToLoad {
bookToLoad = [self bookOrder:bookToLoad];
//---get the path to the property list file---
plistFileNameConf = [[self documentsPath] stringByAppendingPathComponent:#"Config.plist"];
//---if the property list file can be found---
if ([[NSFileManager defaultManager] fileExistsAtPath:plistFileNameConf]) {
//---load the content of the property list file into a NSDictionary object---
dict = [[NSDictionary alloc] initWithContentsOfFile:plistFileNameConf];
bnames = [dict valueForKey:#"BookNames"];
[dict release];
}
else {
//---load the property list from the Resources folder---
NSString *pListPath = [[NSBundle mainBundle] pathForResource:#"Config" ofType:#"plist"];
dict = [[NSDictionary alloc] initWithContentsOfFile:pListPath];
bnames = [dict valueForKey:#"BookNames"];
[dict release];
}
plistFileNameConf = nil;
NSString *bookNameTemp;
bookNameTemp = [bnames objectAtIndex:bookToLoad - 1];
NSLog(#"bookName: %#", bookNameTemp);
return bookNameTemp;
}
You need to allocate your array properly:
bnames = [[NSArray alloc] initWithArray:[dict valueForKey:#"BookNames"]];
Double check that your dict returns the right data type.
There does not appear to be anything wrong with the way you allocate NSDictionary (although you could also use the [NSDictionary dictionaryWithContentsOfFile:] and save yourself having to worry about the release.
Either way I would suggest the issue is not with the [release] but probably the line BEFORE release:
bnames = [dict valueForKey:#"BookNames"];
a) Where is that allocated. I don't see an allocation or declaration of it anywhere?
b) What type of value do you expect back?
Put a break point on it and make sure your getting what you expect or anything.
If dict is not already a strong property, make it one. Then, use self.dict when assigning to it (and keep the release).
I've found what appears to be a better solution to the issue. This lets iOS manage the memory.
//---finds the path to the application's Documents directory---
- (NSString *) documentsPath {
NSLog(#"Start documentsPath");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
// NSLog(#"Found documentsPath 40");
NSLog(#"End documentsPath");
return documentsDir;
}
- (NSString *) configPath {
NSLog(#"Start configPath");
NSString *plistFileNameConf = [[self documentsPath] stringByAppendingPathComponent:#"Config.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistFileNameConf]) {
plistFileNameConf = [[NSBundle mainBundle] pathForResource:#"Config" ofType:#"plist"];
}
NSLog(#"plistFile: %#",plistFileNameConf);
NSLog(#"End configPath");
return plistFileNameConf;
}
The following calls the above code as necessary:
NSString *Choice;
NSArray *properties;
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:[self configPath]];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp) {
NSLog(#"Error reading plist: %#, format: %d", errorDesc, format);
}
Choice = [temp objectForKey:#"Choice"];
properties = [temp objectForKey:Choice];