Thursday, December 10, 2015

JSON vs XML which one is better?

JSON
  - good for web services
  - human readable format
  - it uses javascript eval() function which may cause some security flaw as we can excecute the js object

XML
  - good for configurations
  - Using XPath path, we can directly access the element
  - Using XSLT template, we can easily convert from Xml to json, csv or any format


You can go with either JSON or XML basing on your requirement. Many developers think that JSON object is a light weight and which is better than XML. But in my opinion both are same.


Wednesday, August 19, 2015

str, unicode and basestring - Python

Recently I faced one issue on Ubuntu 13.10 in python 2.7. I would like to share it here. the issue is regarding str and unicode type of strings.

The scenario is I am checking for the type of a input, as I am expecting  either string or dictionary (json object). So to know whether the input is string or dictionary, I am using python isinstance and checking for str and dict types. It was working fine in my local machine and failing in our dev environment for str type. The only difference between my local setup and dev envidonrment is Ubuntu OS version. In my local setup it was Ubuntu 13.10 and in Dev it is Ubuntu 14.04.

After struggling lot of hours finally found the issue is with isinstance for string data type. In Ubuntu 13.10 setup isinstance  is returning success for string type and in Ubuntu 14.04 it was not, because in 14.04 string data type becomes unicode and not str. I am just checking for str and not checking for unicode. To avoid this problem we can check for basestring it will work for str and unicode and OS version independent.

In python basestring is the parent for str and unicode. And unicode and str are siblings to each other. Check the below same python code.

str_ip = "checking for str type"
unicode_ip = u'checking for unicode type'

# checking for str type
if isinstance(str_ip, str):
    print "string type is str"
# checking for unicode type    
if isinstance(unicode_ip, unicode):
    print "string type unicode str"
    
# checking string  for base string 
if isinstance(str_ip, basestring):
    print "str_ip is a string"
# checking unicode for base string    
if isinstance(unicode_ip, basestring):
    print "unicode_ip is a string"

Happpy Coding!!!

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