0

I am very new to Objective-C and am attempting a simple xcode project. As a part of this project I implemented a new class to store data. It is supposed to allow for access to a number of class variables. The .h file simply declares the methods seem below. The .m file has the following contents. The problem is that xcode gives a warning that there is 'no setter method for "setNameString1:" for assignment to property'. I haven't been able to find a setter that doesn't involve '[nameString1 release];' and because I am using ARC I cannot use this to clear the string. I know that the setter method is incomplete but I wanted to understand this issue before filling in the rest.

@interface data_storage ()
{

}

+ (NSString *) nameString1;
+ (NSString *) nameString2;
+ (NSString *) nameString3;
+ (NSString *) nameString4;
+ (NSString *) nameString5;
+ (NSString *) nameString6;
@end

@implementation data_storage
+ (NSString *) getter : (int)stringNumber {
    switch (stringNumber) {
        case 1:
            return data_storage.nameString1;
        case 2:
            return data_storage.nameString2;
        case 3:
            return data_storage.nameString3;
        case 4:
            return data_storage.nameString4;
        case 5:
            return data_storage.nameString5;
        case 6:
            return data_storage.nameString6;

        default:
            return nil;
    }
}
+ (void) setter:(int)stringNumber string:(NSString *)nameString {
    switch (stringNumber) {
        case 1:
            data_storage.nameString1 = nameString;
            break;
        case 2:
            data_storage.nameString2 = nameString;
            break;
        default:
            break;
    }
}
@end
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 3
    You need to read some more before you attempt to write any more Objective-C. In particular, read up on "properties". With "properties" you can get the compiler to generate your getters and setters for you. – Hot Licks Jul 13 '13 at 01:57
  • 2
    (Also understand that you're defining *class* methods above (`+` prefix) and they inherently *do not* have access to any data in objects of the class.) – Hot Licks Jul 13 '13 at 02:00
  • voting to close, as this is leading no where... – Anoop Vaidya Jul 13 '13 at 03:26

1 Answers1

2

Hm, well. Definitely interesting, what you're trying to do here. What programming language are you coming from?

The error you're specifically seeing, "no setter method for 'setNameString1'", is because there is no setter for a variable called nameString1. In fact, there are no properties or ivars called nameString1, which is more the problem.

When you do this: + (NSString *) nameString1; you are not actually declaring a variable; you are declaring a class method. To declare a class variable, the usual Objective-C technique I have seen is to use a static variable, a la static NSString *classString = @"This is a class-level string variable!"; This is how I have always done class-level variables, in the rare occasion that I needed to, and here someone else seems to do that as well. As he says in the link, Objective-C really has no notion of class-level variables anyway.

Now, one you declare this "class variable", you can make a getter and setter for it, and that's where your class-level methods come in. I imagine it would go something like this:

+ (NSString *)classString {
    return classString;
}

+ (void)setClassString:(NSString *)newString {
    classString = newString;
}

There, you have class-level getters and setters! Now, you might also want to do classString = [newString copy] in your setter method, and actually, I'm not really sure how ARC handles this sort of thing, but I presume your memory management will all be handled for you.

Now, does this illuminate things a little bit?

Going further, you have these...switching setters and getters, that take an int argument. So, now what are you doing inside these methods? You are doing return data_storage.nameString1; or data_storage.nameString1 = nameString;, which are themselves getters and setters. Keep in mind that:

return data_storage.nameString1 == return [data_storage nameString1] and data_storage.nameString1 = nameString == [dataStorage setNameString1:nameString]

The dot syntax is just neat, under the hood it's calling methods. Now look at your class again--do you have method definitions for each of those? You don't, and that's where your "no setter method for 'setNameString1'" error is coming from.


Okay, now, all that aside, I think you should maybe rethink how you're going about this. What are you trying to accomplish? You want an object that holds some kind of data, and you think you want a class because you want only one of those objects to ever exist? In that case, you should use a Singleton! Here is a good little bit of sample code, here is a tutorial, and you can just use Google to find more information. Let me know if I need to clarify.

Community
  • 1
  • 1
Matt Mc
  • 8,882
  • 6
  • 53
  • 89