Monday, 26 March 2018

Basic Info On Objective C


Object Creation in objective c

MyClass *objectName = [[MyClass alloc]init] ;

Methods declaration in objective c

-(returnType)methodName:(typeName) variable1 :(typeName)variable2;
-(int) max:(int)num1 andNum2:(int)num2;

How to call

-(void)calculateAreaForRectangleWithLength:(CGfloat)length andBreadth:(CGfloat)breadth;



To call this method in the same class, we use the following statement −

[self calculateAreaForRectangleWithLength:30 andBreadth:20];
As said above, the use of andBreadth helps us understand that breadth is 20. Self is used to specify that it's a class method.

Important points



  • By default, Objective-C uses call by value to pass arguments. In general, this means that code within a function cannot alter the arguments used to call the function.
  • Blocks
    Blocks are a language-level feature added to C, Objective-C and C++ which allow you to create distinct segments of code that can be passed around to methods or functions as if they were values. Blocks are Objective-C objects which means they can be added to collections like NSArray or NSDictionary. They also have the ability to capture values from the enclosing scope, making them similar to closures or lambdas in other programming languages
  • Simple Block declaration syntax
    returntype (^blockName)(argumentType);
  • Simple block implementation
    returntype (^blockName)(argumentType)= ^{
    };
  • structure
  • structure is another user-defined data type available in Objective-C programming which allows you to combine data items of different kinds.
    Example
  • struct Books
    {
       NSString *title;
       NSString *author;
       NSString *subject;
       int   book_id;
    };
  • struct Books Book1;        /* Declare Book1 of type Book */
     

       /* book 1 specification */
       Book1.title = @"Objective-C Programming";
       Book1.author = @"Nuha Ali";
       Book1.subject = @"Objective-C Programming Tutorial";
       Book1.book_id = 6495407;

     //Printing

     /* print Book1 info */
       NSLog(@"Book 1 title : %@\n", Book1.title);
       NSLog(@"Book 1 author : %@\n", Book1.author);
       NSLog(@"Book 1 subject : %@\n", Book1.subject);
       NSLog(@"Book 1 book_id : %d\n", Book1.book_id);
      
  • Structures can be passed as Function Arguments
  • struct Books Book1;  
  • /* book 1 specification */
       Book1.title = @"Objective-C Programming";
       Book1.author = @"Nuha Ali";
       Book1.subject = @"Objective-C Programming Tutorial";
       Book1.book_id = 6495407;
  •    Method
       - (void) printBook:( struct Books) book
    {
       NSLog(@"Book title : %@\n", book.title);
       NSLog(@"Book author : %@\n", book.author);
       NSLog(@"Book subject : %@\n", book.subject);
       NSLog(@"Book book_id : %d\n", book.book_id);
    }

  •  SampleClass *sampleClass = [[SampleClass alloc]init];
       /* print Book1 info */
       [sampleClass printBook: Book1];
  •    Pointers to Structures
    You can define pointers to structures in very similar way as you define pointer to any other variable as follows:
  • More info in the following link
  • Objective-C Categories
  • Sometimes, you may find that you wish to extend an existing class by adding behavior that is useful only in certain situations. In order add such extension to existing classes, Objective-C provides categories and extensions.
  • Syntax Example
    @interface ClassName (CategoryName)
  • id can be used to represent any kind of data type. It’s the most generic form of specifying data; it simply stands for an “identifier” that is used to reference data. This type of dynamic typing allows for increased flexibility in how we code certain things. In iOS, we often see id used for methods that are tied to controls on the screen, like:
    - (IBAction)save:(id)sender;
  • But notice that the sender parameter is of type id. This allows some flexibility in that our app doesn’t care if the sender is a button or an image or whatever else we might want to use to trigger this action.
  • To set up external libraries into Android projects I used Gradle build system and dependency definitions which were quite clever and easy to handle. The equivalent tool in iOS development is CocoaPods
  • Activity = ViewController
  • Transitions between Screens
  • startActivity(Intent) = Segue
    Segues serve the same functionality as starting an activity through an intent. One really cool feature that XCode has is the ability to create a transition to another screen without writing any code. It saves some time and you’ll quickly wish Google would do the same.
    Lists
  • ListView/RecyclerView = TableView
  • Simply create a TableView or TableViewController to get the same list functionality that you’re used to. This also has methods for populating each item in the list similar to Adapters in Android.
  • APK = Archive
  • Interfaces = Archive
  • Emulator = Simulator
  • How to display float in textfield in IOS
    [NSString stringwithformat:@"%f",floafvalue]
  • int i = [self.textfield.text intValue]

Class Methods

Class methods can be accessed directly without creating objects for the class. They don't have any variables and objects associated with it. An example is shown below.

+(void)simpleClassMethod;
It can be accessed by using the class name (let's assume the class name as MyClass) as follows −

[MyClass simpleClassMethod];

Instance Methods

Instance methods can be accessed only after creating an object for the class. Memory is allocated to the instance variables. An example instance method is shown below.
-(void)simpleInstanceMethod; 
It can be accessed after creating an object for the class as follows −
MyClass  *objectName = [[MyClass alloc]init] ;
[objectName simpleInstanceMethod];

Short Cuts in Xcode
ctrl+cmd+up arrow to jump b/w headre and implementation
cmd+i to launch instruments
cmd+r to run
ARC to memory management
Go to Edit section in the toolbar menu select refactor the select ARC
cmd+b to build


------------------------------------------------
In mac for xcode
options+cmd+enter to open assistant editor
ctrl+mouse click for right click
ctrl+i to reindent code
cmd+/ for comments
cmd+shift+k to clean projects










Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...