Beau D'Amore
Beau D'Amore

Reputation: 3404

iOS: Custom class import

I moved some code that will be used multiple times into a class. I'm not getting errors, but I'm also not getting results. It seems to skip over my class completely. Ideally, this class is supposed to do NSURL conns and XMLParser stuff to chew up the data feed from our hosting API. I already have this working but wanted to congeal and somewhat normalize/centralize some of the main logic of my code.

The one function 'bdCheckIfFileExistsAndisValid' is supposed to take a string but return BOOL and it isn't being called at all.

Neither is 'bdParsePlaylistXML' that is supposed to take a string and return an array.

I put breakpoints everywhere in my class and none are hit. I'm new so I'm not sure if I did everything right. Here's some code, thanks in advance.

--------------------CUSTOM CLASS:(.h)

@interface bdXMLParser : NSObject {

    NSMutableArray *playlist;

    //Playlist XML info
    BOOL recordTrackName;
    BOOL recordTrackDescription;
    BOOL recordTrackThumbnailAbsoluteLocation;
    BOOL recordTrackURL;
    NSString *TrackName;
    NSString *TrackDescription;
    NSString *TrackThumbnailAbsoluteLocation;
    NSString *TrackURL;

}

-(NSMutableArray*) bdParsePlaylistXML:(NSString *) playlistXMLFileName;

-(BOOL) bdCheckIfFileExistsAndisValid:(NSString *) localFileName;

----------------CUSTOM CLASS (.m):

#import "bdXMLParser.h"

@implementation bdXMLParser
{
    NSMutableData *webData;

    NSMutableArray *playlist;
    NSXMLParser *xmlParserPlaylist;    
}

-(NSString*) bdDocumentsDirectory{
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    return documentsPath;
}

-(int) bdCheckFileCreationDate:(NSString *) fileName {

    //get XML file path
    NSString *localFilePath = [[self bdDocumentsDirectory] stringByAppendingPathComponent:fileName];

    //local file check
    NSFileManager *filemgr;
    filemgr = [NSFileManager defaultManager];
    NSDictionary* attrs = [filemgr attributesOfItemAtPath:localFilePath error:nil];
    NSDate *fileCreationDate = [attrs objectForKey: NSFileCreationDate];
    NSDate *rightNow = [NSDate date];
    NSTimeInterval lastDiff = [fileCreationDate timeIntervalSinceNow];
    int lastDiffINT = round(lastDiff);

    NSLog(@"NSFileCreationDate:%@",fileCreationDate);
    NSLog(@"CurrentDate:%@",rightNow);
    NSLog(@"lastDiff:%f",lastDiff);

    return lastDiffINT;    
}

-(BOOL) bdCheckIfFileExistsAndisValid:(NSString *) fileName {    
    //local file check    
    NSString* foofile = [[self bdDocumentsDirectory] stringByAppendingPathComponent:fileName];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];

    if ((fileExists == YES) && ([self bdCheckFileCreationDate:foofile] > -86400))//(24 hrs = 86400 seconds)
        return YES;
    else
        return NO;

}

HERE's THE VIEW WHERE I'M TRYING TO USE IT:(menu.h)

#import "bdXMLParser.h"
@interface MenuScreenViewController : UIViewController <NSXMLParserDelegate>
- (IBAction)btnPlayerPlayPause:(id)sender;

(menu.m)

- (IBAction)btnPlayerPlayPause:(id)sender {
    //if array exists, don't reload xml, dont reparse xml, just go to the view
    if (playlist.count == 0){

        //Playlist!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //========================================================================
        //1st: check to see if we have a local cached xml data
        //if we do, check if it is <24hr old and if so load it
        //if not, go get it with connection and overwrite/store it

        //init blogs NSMutableArray
        playlist = [[NSMutableArray alloc] init];

        //local file check
        bdXMLParser *myParser;
        BOOL fileExistsAndValid = NO;
=HERE!==fileExistsAndValid = [myParser bdCheckIfFileExistsAndisValid:PlaylistName];        

        //1st
        if (fileExistsAndValid)//(<24 hrs old)
        {
            NSLog (@"File fileExistsAndValid");

=AND HERE!!=playlist = [myParser bdParsePlaylistXML:PlaylistName];
            NSLog(@"playlist:%u", playlist.count);
            //load first track
            [self LoadTrack:0];
        }
        else{

            NSLog (@"File doesn't exist");
            //call refresh function
            //[self refreshAlbumPhotoXML];
            [myParser bdRefreshPlaylistXML];
        }
    }   
}

Upvotes: 0

Views: 293

Answers (1)

skytz
skytz

Reputation: 2201

you forgot initing the class

bdXMLParser *myParser= [[bdXMLParster alloc]init];

Upvotes: 1

Related Questions