Problem with NSString

Discussion in 'Mac Programming' started by wgshiver, Jul 14, 2011.

  1. wgshiver macrumors newbie

    Joined:
    Jul 6, 2011
    #1
    Alright I am having a crazy problem with this thing.

    I declare an NSString in my App Delegate and during the loading phase everything works fine.

    But after the program loads I can no longer access that NSString. Its like the program deletes all references to it. I am so confused. I have no idea why it would do that. I thought the App Delegate always stayed active.

    Here is the code. Now I do create a new window in the applicationDidFinishLaunching method but I assume the App Delegate is still opened even with this new window.

    Here is the code

    WebRefreshAppDelegate.h
    Code:
    #import <Cocoa/Cocoa.h>
    #import <WebKit/WebKit.h>
    #import "PlayerInformation.h"
    
    @interface WebRefreshAppDelegate : NSObject <NSApplicationDelegate> {
    @private
        NSWindow *window;
        WebView *webView;
        
        NSTimer *timer;
        
        PlayerInformation *player;
     
        NSString *globalHash;
     
    }
    
    @property (assign) IBOutlet NSWindow *window;
    @property (nonatomic, retain) NSString *globalHash;
    
    
    -(void)executeTimer;
    -(BOOL)checkHash;
    -(void)reloadWebPage;
    
    @end
    
    
    
    WebRefreshAppDelegate.m

    Code:
    #import "WebRefreshAppDelegate.h"
    #import "JSON.h"
    #import "PlayerInformation.h"
    
    @implementation WebRefreshAppDelegate
    
    @synthesize window;
    @synthesize globalHash;
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        // Insert code here to initialize your application
        globalHash = nil;
        [self checkHash]; [B] //WORKS FINE ON THIS CALL[/B]
        [self executeTimer];
        int windowLevel = CGShieldingWindowLevel();
        
        NSRect screenRect = [[NSScreen mainScreen] frame];
        window = [[NSWindow alloc] initWithContentRect:screenRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO screen:[NSScreen mainScreen]];
        
        
        NSView *content = [window contentView];
        [content retain];
            
        webView = [[WebView alloc] initWithFrame:screenRect];
        [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://admin.hypersign.net/players/view/id:1303905558/w:1920/h:1080"]]];
        [window setContentView:webView];
        [window makeKeyAndOrderFront:nil];
    
        //[window setContentView:content];
        [window makeFirstResponder:content];
        //[window setLevel:windowLevel];
        
        
    
    }
    
    -(void)dealloc
    {
        [super dealloc];
    }
    -(void)executeTimer
    {
       
        
        if(timer == nil)
        {
            NSLog(@"Starting timer");
            
            timer = [[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(reloadWebPage) userInfo:nil repeats:YES] retain];
            
        }
        
        
    }
     
    -(BOOL)checkHash
    {
        [B]//THIS IS WHERE IT FAILS ON THE SECOND RUN THROUGH. IT WONT PRINT[/B]
        NSLog(@"Got in Check Hash and hash is %@", globalHash);
        NSString *localHash;
        NSString *currentHash;
        NSString *url = [NSString stringWithFormat:@"http://admin.hypersign.net/players/view/id:1303905558/w:1920/h:1080/hash:%@", globalHash];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
        
        NSHTTPURLResponse *response;
        
        NSLog(@"Sending Hash Request");
        
        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
        
        if([response respondsToSelector:@selector(allHeaderFields)])
        {
            NSDictionary *dictionary = [response allHeaderFields];
            
            NSString *jsonString = [dictionary objectForKey:@"X-Json"];
            
            NSLog(@"%@", [jsonString description]);
            
            if(jsonString)
            {	
                NSDictionary *json_results = [jsonString JSONValue];
                
                //NSArray *json_results = [jsonString JSONValue];
                
                localHash = [json_results objectForKey:@"content_hash"];
                currentHash = [NSString stringWithFormat:@"%@", localHash];
            }
        }
        
        NSLog(@"finished with hash request");
        
        if([currentHash isEqualTo:[player getPlayerHash]] && [player getPlayerHash] != nil)
        {
            return false;
        }
        else if(localHash != nil)
        {
            globalHash = [NSString stringWithFormat:@"%@", localHash];
            
        }
        
        NSLog(@"hash is: %@", globalHash);
        return true;
               
           
    }
    
    -(void)reloadWebPage
    {
        //[[webView mainFrame] reload];  
        if([self checkHash])
        {
            [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
        }
        else
        {
            //do nothing. That means the hash values were equal and it returned false
        }
    }
    @end
    
    So basically the code initializes a few variables. Opens up a new window that is a web browser and starts a time. When the timer ticks every 15 seconds it grabs a hash and compares the hashes if they are different it refreshes the web page.

    It is failing when I try to print out globalHash the second time.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    globalHash is a property. You are not using property syntax to access it on this line:

    Code:
    globalHash = [NSString stringWithFormat:@"%@", localHash];
    As you are not using the property syntax this will not call the setter, rather it simply sets the pointer to point at the object. The object in this case is autoreleased and as you have not called the setter it is not being retained.

    You should use
    Code:
    self.globalHash = [NSString stringWithFormat:@"%@", localHash];
    This is probably the best argument for giving your variables a different name to the property. For example .h
    Code:
    @interface MyObject
    {
    NSString *_myString;
    }
    @property (nonatomic, retain) NSString *myString;
    @end
    
    .m
    Code:
    @implementation MyObject
    @synthesize myString= _myString
    @end
    
     
  3. wgshiver thread starter macrumors newbie

    Joined:
    Jul 6, 2011
    #3
    Thank you robbieduncan!

    That worked perfectly. I learned something new. This is my first real program in Objective-C so I'm learning something new about it everyday.

    Thanks again!
     

Share This Page