-2

I created a getter to get the content of a variable, but it doesn't works.

Here the code:

-(void)documentURLReceived:(NSURL *)url{

    _getUrl = [[NSURL alloc] init];
    _getUrl = url;
    NSLog(@"Result: %@", _getUrl);
}
-(void)getUrl{
    NSLog(@"getUrl: %@", _getUrl);
}

Here the result in the consol:

Result: http://www.google.com

getUrl : (null)

I don't understand why!

Here my property:

@property (strong, nonatomic) NSURL *getUrl;

Thanks for your help :)

6
  • 1
    where you call [self getUrl]; method? Commented Apr 4, 2013 at 11:58
  • 1
    check apples docs for naming conventions and memory management. both is important. Commented Apr 4, 2013 at 12:00
  • I'm using ARC. I call this methods in another viewcontroller: MyViewController *main = [[MyViewController alloc] init]; [main getUrl];
    – Lapinou
    Commented Apr 4, 2013 at 12:04
  • Waiting for the answer....... Commented Apr 4, 2013 at 12:05
  • In that case - get rid of the getURL method as you are overwriting the automatically synthesised getter/setters created by the compiler. Also your method getURL is a void method returning nothing. Commented Apr 4, 2013 at 12:05

2 Answers 2

2

If you are using ARC and the latest Xcode - get rid of the getURL method - you should not be overwriting this unless you need to do custom logic, furthermore, your getURL method returns nothing and is a void method - so the variable is most likely set but your method is overwriting the automatically generated getter and is returning nothing.

Did a test in Xcode

In header file I have:

@property (nonatomic, strong) NSURL *getURL;

and then in implementation file I have:

- (void)documentURLReceived:(NSURL *)url
{
    _getURL = url;
    NSLog(@"%@",_getURL);
}

which outputs:

2013-04-04 23:29:27.681 VitalityDesignTestSuite[90518:c07] http://www.google.com

okie dokie I wrote you a sample:

http://bit.ly/10yWb36

to use those you can go:

MyViewController *mvc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
//Now you can set the myURL variable directly
[mvc setMyURL:[NSURL URLWithString:@"http://www.google.com"]];
NSLog(@"Direct Setting: %@",mvc.myURL);

//OR you can call the method your wrote
[mvc documentDidReceiveURL:[NSURL URLWithString:@"http://www.google.com"]];
NSLog(@"Selector Setting: %@",mvc.myURL);

that outputs:

2013-04-04 23:39:09.407 VitalityDesign[92197:c07] Direct Setting: http://www.google.com 2013-04-04 23:39:09.408 VitalityDesign[92197:c07] Selector Setting: http://www.google.com

hope this helps

8
  • I tried to use the method automatically generate by XCode, but it doesn't works :/
    – Lapinou
    Commented Apr 4, 2013 at 12:19
  • @Lapinou: try changing the name of method -(void)getUrl{ to -(void)getUrlMethod{ then say Commented Apr 4, 2013 at 12:21
  • I updated my answer - you sure you have the property set correctly? I can't get it not to work Commented Apr 4, 2013 at 12:27
  • Done. But doesn't works :'( Really, I don't understand why!
    – Lapinou
    Commented Apr 4, 2013 at 12:30
  • is documentURLReceived ever called? I notice earlier you said: MyViewController *main = [[MyViewController alloc] init]; [main getUrl]; -- assuming you got rid of the getURL selector which is good - however - when does this variable actually get set? Also your NSURL will be nil -- i'll just make you a sample gimme a sec... Commented Apr 4, 2013 at 12:33
-3

First of all are you getting anything in url?

If No, -> Work over it

If yes then, try to do following:

_getUrl = [[NSURL alloc] init];
_getUrl = url;
[_getUrl retain];
NSLog(@"Result: %@", _getUrl);

Enjoy Programming!

13
  • You (and the OP) are leaking the empty url created on the first line when assinging a new value on the second line. Commented Apr 4, 2013 at 12:01
  • I do know very well @DavidRönnqvist,but sometimes this trick works for me.. Commented Apr 4, 2013 at 12:02
  • @ForamMukundShah, you should investigate why this trick works for you. as it is just wrong Commented Apr 4, 2013 at 12:03
  • 1
    Throw in an extra retain here and there until you no longer crash from zombies? Commented Apr 4, 2013 at 12:05
  • yup, but I recommend @Lapinou, to try this once. I want to check this trick is working for him also or not. As far as I investigate this will work for him too Commented Apr 4, 2013 at 12:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.