Objective-C: Variable won't get used (if)

how to declare global variable in objective-c
objective-c class variable
declare string in objective-c
objective-c define constant
interface and implementation in objective-c
objective-c syntax
blocks in objective-c
objective-c method declaration

I'm using a pretty simple if-clause which just checks a name an should give back a variable, but it doesnt get recognized. the "createUser" variable is marked as unused.

NSString *salesPersonName = [[[SevdeskApiManager sharedManager] currentSevUser] firstName];
    if ([salesPersonName isEqual: @"XXX "]) {
        NSString *createUser = @"1";
    } else if ([salesPersonName isEqual: @"XXXX"]) {
        NSString *createUser = @"11";
    } else if ([salesPersonName isEqual: @"XXXXX"]) {
        NSString *createUser = @"111";
    } else {
        NSString *createUser = @"1111";
    }
    // Hier die keyvalues eintragen
    NSString *bodyString = [NSString stringWithFormat:@"name=nachtelefonieren&assigned[id]%@=&assigned[objectName]=SevUser&category[id]=31&category[objectName]=Category& createUser[id]=%@&createUser[objectName]=SevUser&noticeCreator=0&deadline=%@&status=100&object[id]=28243&object[objectName]=SevClient",assignedUser,createUser,deadline];

Just move your variable declaration outside the scope of each if (right now the variable is created & discarded immediately)

NSString *createUser;

if ([salesPersonName isEqual: @"XXX "]) {
    createUser = @"1";
} else if ([salesPersonName isEqual: @"XXXX"]) {
    createUser = @"11";
} else if ([salesPersonName isEqual: @"XXXXX"]) {
    createUser = @"111";
} else {
    createUser = @"1111";
}

Also, instead of a multi-if statement, you could use a dictionary:

NSDictionary *dict = @{
    @"XXX " : @"1",
    @"XXXX" : @"11",
    @"XXXXX" : @"111"
};

NSString *createUser = dict[salesPersonName] ?: @"1111";

1.7. Declaring Variables in Objective-C, Declaring Variables in Objective-C Problem You want to use variables with any sense to anybody else but yourself and chances are that they won't make  In programming, a variable is basically a container used to hold data. The data itself can be anything from a simple number to the lyrics and metadata about a song. When we create a variable in Objective-C, we usually specify what kind of data it will hold. This is known as statically typing the data. Blocks of memory in the computer are allocated as the “container” to hold the data, and we store a reference to that container with a name that we provide.

Just declare the createUser instance variable outside of the if-block scope (i.e. similar to your declaration of salesPersonName). This should reduce redundancy in your code and resolve the issue.

Declaration/definition of variables locations in ObjectiveC?, I can understand your confusion. Especially since recent updates to Xcode and the new LLVM compiler changed the way ivars and properties  Thankfully, Objective-C provides a special storage type modifier, named __block, which when is used in a variable declaration, it makes this variable mutable so blocks can have write access to it. In our example, our problem can be solved really easy, simply by making this line:

Declare the instance variable outside of the if-block and init it with an empty value, because it could crash the program at bodyString variable line.

NSString *createUser = @"";
if ([salesPersonName isEqual: @"XXX "]) {
    createUser = @"1";
} else if ([salesPersonName isEqual: @"XXXX"]) {
    createUser = @"11";
} else if ([salesPersonName isEqual: @"XXXXX"]) {
    createUser = @"111";
} else {
    createUser = @"1111";
}

// Hier die keyvalues eintragen
NSString *bodyString = [NSString stringWithFormat:@"name=nachtelefonieren&assigned[id]%@=&assigned[objectName]=SevUser&category[id]=31&category[objectName]=Category& createUser[id]=%@&createUser[objectName]=SevUser&noticeCreator=0&deadline=%@&status=100&object[id]=28243&object[objectName]=SevClient",assignedUser,createUser,deadline];

What is an instance variable in objective c?, program but it can be defined only once in a file, a function or a block of code. Maybe this will be obviously simple for most of you, but could you please give an example how to create similar methods (in Objective-C) and functions in C to create functions like NSString's stringWithFormat:, or NSLog(). Just to remind:

Interview Questions: What are properties and instance variables in , will automatically define what's called a setter and a getter for you automatically. Objective-C is an object-oriented programming language, and is a layer over the C programming language. This means that if you know how to write C, there are only a few syntax changes to learn. This means that if you know how to write C, there are only a few syntax changes to learn.

Working with Blocks, are used to store data in instances of classes. They define memory management, type, and access attributes of the values they store such as strong , weak , assign , readonly , readwrite , etc. Objective C is an object-oriented programming language. It is powerful, flexible and can be used to develop outstanding apps. You will find it easy to learn Objective C if you have learned C or C++ before. If you’re a beginner, you will have to get a good grounding on the basics to pick it up.

Define Variables and Methods of Class in Objective-C, objects, which means they can be added to collections like NSArray or NSDictionary . Objective-C - Pointer arithmetic. There are four arithmetic operators that can be used on pointers: ++, --, +, - 2: Objective-C - Array of pointers. You can define arrays to hold a number of pointers. 3: Objective-C - Pointer to pointer. Objective-C allows you to have pointer on a pointer and so on. 4: Passing pointers to functions in Objective-C

Comments
  • You're creating three completely independent variables named createUser that aren't used within their scope, and the compiler warns you accordingly. These variables shadow your existing createUser variable, otherwise you'd get an "unknown identifier" compiler error in the last line of your sample.