IOS Coding Tips for Beginners

1. Create view objects in Interface builder not in source code.

2. Flower brackets always be there in if conditions or while loops, even it has single statement.

3. Give one line space between @implementation and @synthesize

4. Open flower bracket “{” should start in next line.

5. Give one line space above conditional or loop statements like if, while, for.

6. The method name itself should resembles its behavior.

7. Remove unnecessary methods which are created by the file templates.

8. Use Edit->Format->Re-Indent to re-indent the selection of statements.

9. Don’t use string literals as keys, make the string literal as constant and use that. Place all constants in a common file.

10. Try to use property directly than calling the accessory method. for example Object.memberVariable than [Object memberVariable];

11. Try to define all literals in common file.

12. Instead of using literals, create meaningful constants for them in common file and use them.

13. Always group the things like constants, statements, etc.

14. The format for method signature should be like
– (void) methodName:(id) sender;

15. One line space between logical group of statements and two lines between physical groups.

16. Try to eliminate autorelease objects, instead use retain and release.

17. If you are declaring a UI object, please suffix the type to the variable. For example, if you are declaring a UILabel object with name ‘userName’, then name it ‘userNameLabel’.

18. Maintain all image names in lowercase with underscore dividing the inline words.

19. Directly access the variable instead of using property in the same class. For example, don’t access

UITableView mTableView;
@Property () UITableView *tableView;
//To reload the table in the same class use
[mTableView reloadData]; // correct
[self.tableView reloadData]; //wrong

Advertisements

IOS Coding Tips

1. Create view objects in Interface builder not in source code.

2. Flower brackets always be there in if conditions or while loops, even it has single statement.

3. Give one line space between @implementation and @synthesize

4. Open flower bracket “{” should start in next line.

5. Give one line space above conditional or loop statements like if, while, for.

6. The method name itself should resembles its behavior.

7. Remove unnecessary methods which are created by the file templates.

8. Use Edit->Format->Re-Indent to re-indent the selection of statements.

9. Don’t use string literals as keys, make the string literal as constant and use that. Place all constants in a common file.

10. Try to use property directly than calling the accessory method. for example Object.memberVariable than [Object memberVariable];

11. Try to define all literals in common file.

12. Instead of using literals, create meaningful constants for them in common file and use them.

13. Always group the things like constants, statements, etc.

14. The format for method signature should be like
        – (void) methodName:(id) sender;

15. One line space between logical group of statements and two lines between physical groups.

16. Try to eliminate autorelease objects, instead use retain and release.

17. If you are declaring a UI object, please suffix the type to the variable. For example, if you are declaring a UILabel object with name ‘userName’, then name it ‘userNameLabel’.

18. Maintain all image names in lowercase with underscore dividing the inline words.

19. Directly access the variable instead of using property in the same class. For example, don’t access

UITableView mTableView;
@Property () UITableView *tableView;
//To reload the table in the same class use
[mTableView reloadData]; // correct
[self.tableView reloadData]; //wrong

How to check the character is backspace (delete)


- (BOOL) textField:(UITextField *) textField
shouldChangeCharactersInRange:(NSRange) range
            replacementString:(NSString *) string
{
     if ([string length])
     {
          if ([string characterAtIndex:0] == 10)
          {
               NSLog(@"keyboard backspace pressed");
          }
      }
      return YES;
}

Blogged with the Flock Browser

Basic topics a mac developer should know

  • MVC Pattern
  • Memory Management
  • Archiving Data
  • User Defaults
  • Localization
  • Categories
  • Formal and informal Protocols
  • Key Value coding
  • Key Value observing
  • Custom Drawing
  • Notifications
  • Delegation
  • Blocks
  • Threads
  • Core Animation
  • Debugging
  • Instruments
  • Predicates
  • Custom Drawing
  • Bindings
Blogged with the Flock Browser

NSURLConnection Usage

– (void)sendRequestToServer:(NSURL *)url httpBody:(NSString *)methodCallString
{

    NSMutableURLRequest *xmlrpcRequest =[NSMutableURLRequest requestWithURL:url
                                                                cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                            timeoutInterval:60.0];
    [xmlrpcRequest setHTTPMethod:@”POST”];
    [xmlrpcRequest setValue:@”text/xml” forHTTPHeaderField:@”Content-Type”];
   
    // set the HTTPBody with the method call string as bytes
    const char *utfString = [methodCallString UTF8String];
   
    NSString *utfStringLenString = [NSString stringWithFormat:@”%u”, strlen(utfString)];
   
    [xmlrpcRequest setHTTPBody:[NSData dataWithBytes: utfString length:strlen(utfString)]];
   
    // Set the Content-Length header
    [xmlrpcRequest setValue:utfStringLenString forHTTPHeaderField:@”Content-Length”];
   
    // create the connection with the request and start loading the data
    if (!isConnectionBusy) {
        isConnectionBusy = YES;
        NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:xmlrpcRequest delegate:self];
       
        if (theConnection) {
            responseData = [[NSMutableData data] retain];
        } else {
            NSLog(@”Connection Failed!”);
        }
    }
}

– (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    if (responseData != NULL)
        [responseData setLength:0];
}

– (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
    //    NSString *responseString = [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding] autorelease];
}

– (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    isConnectionBusy = NO;

    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [responseData release];
       
    // inform the user
    NSLog(@”Connection failed! Error – %@ %@”,
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

– (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    isConnectionBusy = NO;
   
    NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];

    [connection release];
    connection = nil;
    [responseData release];

    [self parseData:responseString];
    [responseString release];
    // release the connection, and the data object
}

-(NSURLRequest *)connection:(NSURLConnection*) connection
            willSendRequest:(NSURLRequest*)    request
           redirectResponse:(NSURLResponse*) redirectResponse
{
    //NSLog(@”connection willSendRequest redirectResponse”);

    NSURLRequest *newRequest = request;
    if (redirectResponse)
    {
        newRequest=nil;
    }
    else
    {
    }
    return newRequest;
}

– (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    return nil;
}

Blogged with the Flock Browser

Escape code for % sign in NSString

The escape code for a percent sign is “%%”, so your code would look like this

[NSString stringWithFormat:@”%d%%”, someDigit];

This is also true for NSLog() and printf() formats.

Cited from http://stackoverflow.com/questions/739682/how-to-add-percent-sign-to-nsstring.

Blogged with the Flock Browser

Warning: Backslash and Newline separated by space

Warning: Backslash and Newline separated by space

Solution: The backslash (\) character is used as the continuation character to continue #define statements and strings to the next line. GCC expects the backslash character to be the very last character on the line. This warning indicates that there is a space after the backslash. Delete the space and any other characters that come after the backslash.

Blogged with the Flock Browser