2012-04-03 75 views
0

有一个很艰难的时期得到一个NSStringNSURL来自NSString的NSUrl?

NSURL *url = [NSURL URLWithString:@"http://.../social.php"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
    for(id entry in JSON) 
    { 
     strTwitter= [entry valueForKeyPath:@"str1"]; 
     strFacebook =[entry valueForKeyPath:@"str2"]; 
     CCLOG(@"twitter: %@", strTwitter); 
    } 
} failure:nil]; 

[operation start]; 
别处

...

然后我用EXC_BAD_ACCESS

NSURL线崩溃mainMenu.h

@interface mainMenuLayer : CCLayer 
{ 
    NSString *strTwitter; 
} 

CCLOG打印URL很好 - 没有空格;有https等...?

.H

#import "cocos2d.h" 

#import "menuHome.h" 
#import "menuMenu.h" 
#import "AFJSONRequestOperation.h" 


@interface mainMenuLayer : CCLayer 
{ 
    CCSprite *Background; 
    CCSprite *logoBackground; 
    CCSprite *logoName; 
    menuHome *theMenuHome1; 
    menuMenu *theMenuMenu1; 

    CGSize size; 
    CCMenu *bottomMenu; 
    CCMenu *bottomMenu2; 
    CCMenuItemImage *menuHouse; 
    CCMenuItemImage *menuMenu2; 
    CCMenuItemImage *menuPictures; 
    CCMenuItemImage *menuContact; 
    CCMenuItemImage *menuMore;  
    CCMenuItemImage *menuTemp; 

    CCMenuItemImage *menuTwitter; 
    CCMenuItemImage *menuFacebook; 
    CCMenuItemImage *menuWebsite; 
    CCMenuItemImage *menuAbout; 
    CCMenuItemImage *menuBlank; 

    NSString *strTwitter; 
    NSString *strFacebook; 

    NSInteger moreVisible; 

} 
// returns a CCScene that contains the HelloWorldLayer as the only child 
+(CCScene *) scene; 

-(void) funcHome; 
-(void) funcMore; 
-(void) funcMenu2; 


+(mainMenuLayer*) sharedMenuScene; 
@end 

.M

#import "mainMenu.h" 
#import "constants.h" 

@interface mainMenuLayer() 
@property(nonatomic, strong) NSString *strTwitter; 
@end 

// HelloWorldLayer implementation 
@implementation mainMenuLayer 

@synthesize strTwitter; 


static mainMenuLayer *sharedMenuScene; 

+(mainMenuLayer*) sharedMenuScene { 
    NSAssert(sharedMenuScene != nil, @"not yet initilized"); 
    return sharedMenuScene; 
} 

+(CCScene *) scene 
{ 
    // 'scene' is an autorelease object. 
    CCScene *scene = [CCScene node]; 

    // 'layer' is an autorelease object. 
    mainMenuLayer *layer = [mainMenuLayer node]; 

    // add layer as a child to scene 
    [scene addChild: layer]; 

    // return the scene 
    return scene; 
} 

// on "init" you need to initialize your instance 
-(id) init 
{ 
    // always call "super" init 
    // Apple recommends to re-assign "self" with the "super" return value 
    if((self=[super init])) { 


     sharedMenuScene = self; 
     // create and initialize a Label 
     self.isTouchEnabled = YES; 

     [[CCDirector sharedDirector]enableRetinaDisplay:YES]; 


     // ask director the the window size 
     size = [[CCDirector sharedDirector] winSize]; 
     moreVisible = 0; 


     //theMenuHome1.position = ccp(0.0f, 0.0f); 

     logoBackground = [CCSprite spriteWithFile:@"logo_bg_640x120.png"];  
     [self addChild:logoBackground z: depthLevelMenu]; 
     logoBackground.position = ccp(size.width/2, size.height - 30); 

     logoName = [CCSprite spriteWithFile:@"res_name.png"];  
     [self addChild:logoName z: depthLevelMenuLogo]; 
     logoName.position = ccp(size.width/2, size.height - 30); 

     Background = [CCSprite spriteWithFile:@"background.png"];  
     [self addChild:Background z: depthLevelBackground]; 
     Background.position = ccp(size.width/2, size.height/2); 

     menuHouse = [[CCMenuItemImage itemFromNormalImage:@"home_128x100_unselect.png" selectedImage:@"home_128x100_select.png" target:self selector:@selector(funcHome:)]retain]; 

     menuHouse.position = ccp(32, 25); 

     menuContact = [[CCMenuItemImage itemFromNormalImage:@"contact_128x100_unselect.png" selectedImage:@"contact_128x100_select.png" target:self selector:@selector(funcHome:)]retain]; 

     menuContact.position = ccp(96, 25); 

     menuMenu2 = [[CCMenuItemImage itemFromNormalImage:@"menu_128x100_unselect.png" selectedImage:@"menu_128x100_select.png" target:self selector:@selector(funcHome:)]retain]; 

     menuMenu2.position = ccp(160, 25); 

     menuPictures = [[CCMenuItemImage itemFromNormalImage:@"photos_128x100_unselect.png" selectedImage:@"photos_128x100_select.png" target:self selector:@selector(funcHome:)]retain]; 

     menuPictures.position = ccp(224, 25); 

     menuMore = [[CCMenuItemImage itemFromNormalImage:@"more_128x100_unselect.png" selectedImage:@"more_128x100_select.png" target:self selector:@selector(funcMore:)]retain]; 

     menuMore.position = ccp(288, 25); 


     bottomMenu = [CCMenu menuWithItems: menuHouse, menuContact, menuMenu2, menuPictures, menuMore, nil]; 
     bottomMenu.position = CGPointZero; 

     [self addChild: bottomMenu z:depthLevelMenu]; 
     [menuHouse selected]; 

     [self getSettings]; 
    } 
    return self; 
} 


-(void) getSettings 
{ 
    NSURL *url = [NSURL URLWithString:@"http://.../social.php"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     for(id entry in JSON) 
     { 
      strTwitter= [entry valueForKeyPath:@"str1"]; 
      strFacebook =[entry valueForKeyPath:@"str2"]; 
      CCLOG(@"twitter: %@", strTwitter); 
     } 
    } failure:nil]; 

    [operation start]; 

} 


-(void) funcMenu2: (id) sender 
{ 
    CCLOG(@"funcMenu2 Called"); 
    [menuTwitter unselected]; 
    [menuFacebook unselected]; 
    [menuWebsite unselected]; 
    [menuAbout unselected]; 
    [menuBlank unselected]; 
    [menuMore unselected]; 


    if(sender == menuTwitter) 
    { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString: strTwitter]]; 
     CCLOG(@"url: %@", strTwitter); 

     CCLOG(@"Twitter Selected"); 
     [sender selected]; 

    } 
} 

回答

3

如何strTwitter实例?看起来是被之前

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: strTwitter]]; 

声明

作出strTwitter私有财产(把这个你.m文件中公布:

@interface mainMenuLayer() 
    @property(nonatomic, strong) NSString *strTwitter; 
@end 

然后右键低于@implementation mainMenuLayer

@synthesize strTwitter; 

另一个需要注意的是,你应该养成习惯 MainMenuLayer VS mainMenuLayer

+0

添加了.h文件 – lsiunsuex 2012-04-03 23:23:41

+0

更新了答案,只要strTwitter NSString对象只能在这个类中引用,你会没事的。如果您需要从另一个类中设置该属性,则需要将@property声明放入.h文件中。 – 2012-04-03 23:33:09

+0

它仍然与EXC_BAD_ACCESS崩溃。我把它完全按照你告诉我的方式放入;甚至试图将该属性放入.h - ? – lsiunsuex 2012-04-03 23:45:16

1

变化:定义类在使用适当的情况下, “strTwitter = [条目valueForKeyPath:@” STR1 “];” 来

self.strTwitter = [条目valueForKeyPath:@ “STR1”];

这将确保它被自动生成的setter保留。分配strTwitter的方式避开了setter,只是将非保留对象分配给它。我习惯于在分配时调用“self。{property} = {some value}”。

+0

这适用于两种方式。测试一下,看看。做到这一点的“正确”方法是将伊娃和合成器加以抑制。 – 2012-04-04 11:17:54