Saturday, June 27, 2015

Attempt to mutate immutable object with appendString - ObjectiveC

"Attempt to mutate immutable object with appendString" is one of the most common error while doing iOS development. This will occur due to mutability of the NSString. Lets check in detail.
As most of you know foundation framework has two types of strings

  • NSString   (not modifiable)
  • NSMutableString (can modify)

Basing on these strings, we can have below two scenarios.

1. We can assign NSMutableString to NSString
2. We can't assign NSString to NSMutableString

Scenario #1 is perfectly valid, because NSMutableString is a subclass of NSString. But scenario #2 will crash the application and throws below error message.

"Attempt to mutate immutable object with appendString"

To avoid this problem , while doing #2, you need to make mutableCopy to make a mutable string and assign.  Check below for sample code.

    //Immutable string , cant modify
    NSString *firstName = @"Pasumarthi";
    //mutable string can modify
    NSMutableString *nameString = [NSMutableString stringWithString:@"Chandu"];
    // invalid - trying to make mutable string to non-mutable string 
    //nameString = firstName;
    //invalid - we cant make normal string copy to the mutable.
    //nameString =  [firstName copy]; 
    // valid as mutablestring is subclass of string
    nameString = [firstName mutableCopy]; 
    // valid as mutablestring is subclass of string    
    firstName  = nameString;
    //valid as nameString is mutable    
    [nameString appendString:@" Appu"];

Happy Coding!!!

Wednesday, June 17, 2015

XCode short cuts!!

When we are working with Integrated Development Kit(IDE) like Xcode, Eclipse, PyCharm etc, Short cuts will help you a lot and increases productivity. So here are the some of the short cuts which will be very useful for the Xcode. When developing iOS application these Xcode short cuts will help you a lot

Xcode is organized into four blocks as below

1. Navigation Area: where all the project files will be displayed in hierarchal order 
2. Editing Area: Where we can add/edit our source code to the files and this will be always visible most of the time, and no need to hide this area.
3. Debug Area: Where we can check the logs/message and stack frames when using break points
4. Utility Area: where we can find the properties of the UI elements and widgets etc.

Special keys on Mac Laptop: Short cuts are combination of multiple keys mostly special keys and characters and numbers.

⌘ - Command, Cmd
⌃ - Control, Ctrl
⌥ - Alt, Option, Opt
⇧ - Shift

Most frequently used  and very usefull Xcode short cuts:

⌘+0 - Navigation area 
⌘+⌥ +0 - Utility area 
⌘+⇧+Y - Debug area 
⌘+⇧+o - File search and opening
⌃+⌘+Up/Down Arrow - switching/changing .m and .h
⌃+⌘+Left/Right Arrow - go forward/back ward
 + 6 - to find a method in a file

Below are some more use full short cuts:

#+1 - Project Navigator
#+2 - Symbol Navigator
#+3 - Find Navigator
#+4 - Issue Navigator
#+5 - Unit Tests Navigator
#+6 - Debug Navigator
#+7 - Break Point Navigator
#+8 - Report Navigator
#+b - Building project
#+r - Running the application
⇧+⌘+k - cleaning the project

Happy Xcoding!!!

Popular Posts