new to ios – not able to assign string to property

| | August 6, 2015

in my .m file i created the following property:

@property(strong) NSString *var;

I tried to assign a string to it:

- (void)viewDidLoad {
    [super viewDidLoad];

   [self.var getstring];

}//viewDidLoad

-(NSString *)getstring{


    return @"hello";

}

but it gave me the error “no visible @interface for NSString declares the selector getString

6 Responses to “new to ios – not able to assign string to property”

  1. The problem is that these square brackets mean “send a message to this object”(obj-c msg send). What do you do? You send a message to your var. Your var is an instance of Nsstring class which in turn does not have method called getstring. That is what error actually says. So, my thoughts are:

    1. The correct way to assign a string to your var is self.var = [self getstring]
    2. It is better to use camel style coding. So the method name looks like getString
  2. When you not use @synthesize,the getter and setter method is not rewrite.In other words,you can only use system’s getter and setter method.But when you use @synthesize,you can rewrite the getter and setter method.

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property(nonatomic)NSString *age;
    @end
    
    @implementation ViewController
    
    
    @synthesize age = _age;
    - (void)viewDidLoad {
        [super viewDidLoad];
    
    }
    -(void)setAge:(NSString *)age {
        _age = age;
    }
    -(NSString *)age
    {
        return _age;
    }
    
    @end
    
  3. @Jack,

    getstring is NSString and its acts like method return.
    In iOS we usually call the method like [self getstring];

    So it seems, you wrote own string method and like to assign the return value for varible ‘var’ right?

    ‘self’ it means or refers in same page declarations.

    So,
    self.var = @”hello”;
    or
    self.var = [self getstring]; // it calls method and return string value will be set for ‘var’ variable. Thanks.

  4. [self.var getstring] 
    

    The code above sends a message/ “calls” the getstring selector/”method” in var, which has a type of NSString. NSString has no such selector which explains your error:

    no visible @interface for NSString declares the selector getString

    To assign the result of getstring to the property var, you would write:

    self.var = [self getstring];
    

    NOTE

    Objective-C discourages prepending methods with get. You could call your method -(NSString *) helloString

  5. - (void)viewDidLoad {
        [super viewDidLoad];
    
       self.var = [self getstring];
    
    }//viewDidLoad
    
    -(NSString *)getstring{
        return @"hello";
    }
    

    Function call into same ViewController for that time we uses [self
    methodName] if return any some value then need to catch that value
    with “=” to assign a value.

    So that here you need to replace this code [self.var getstring]; with self.var = [self getstring];

  6. you just need to do assign the string

    self.var = [self getstring] instead:

    [self.var getstring];

Leave a Reply