How to build a Native iOS React Native Module

It's not that bad, promise

Posted by Garrett Mac on 07/07/7990
In Development, React Native
Tags blog, react native, native module, animation

How to build a Native iOS React Native Module

It’s not that bad, promise

Objective-C basics for Web Developers

Project Starter Files / Structure

When you start a project you’ll start with the following:

AppDelegate.h
AppDelegate.m

ViewController.h
ViewController.m

Everything in the AppDelegate is Global. Any View the user is currently in will have access to the logic inside the AppDelegate.

And together these ViewController.* and AppDelegate.* files create the AppDelegate Class.

Notice how you have an AppDelegate.h as well as another one with a “.m” extension.

Header Files (Files With .h Extensions)

These are called the Header Files. Everything in here is Public.

complies all .c and .m files and replaces any #include *, or #import * directives with the entire text of those files where that directive #include *, or #import * statement was.

Example


Assume you have the following: Example.c

Alice
#include "Example.h"
Wonderland

Example.h

in

Compiled Result

Alice
in
Wonderland

Whats Happening The compiler would, after processing the replaces any #include *, or #import * directives with the entire compiled output would show:

Implementation Files (Files With .m Extensions)

Is called the Implementation Files (where the .m I assume stands for “Model”, seeing as Objective-C uses the MV Architecture), Everything in here is Private. It’s public when it’s methods are included in the .h header file.

These are where your logic goes.

Syntax

Example using NSLog

NSLog

notice:

1. NSLog(@"My Log %@")

%@

Is a Placeholder for Objects but NSString and NSNumber are both classes, and emit object instances of themselves when we create pointers with them

Defining A String:

You’re not supposed to use int, string, const, or char’s in Objective-C

1| NSLog(@"My Log %@")
2| NSString *myString = @"Hello World"
      ^        ^            ^
      |        |            |
  Definition   |      `@` says its a string
               |
         A Pointer You Created

Lets that exand that by adding a number

1| NSLog(@"My Log %@")
2| NSString *myString = @"Hello World"
3| NSNumber *myNumber = [NSNumber numberWithInt:420]
      ^        ^            ^
      |        |            |
  Definition   |      * See Below
               |
         A Pointer You Created
4| NSLog(@"My Log: %@",myString)
output >> "My Log: Hello World"

Notice the scary

[NSNumber numberWithInt:420]

All this is saying is

[<CLASS> <CLASS METHOD>:<PARAM>]

Where you use the [] brackets to call a method of a class.

Now lets log both our numbers and strings

1| //NSLog(@"My Log %@")
2| NSString *myString = @"Hello World"
3| NSNumber *myNumber = [NSNumber numberWithInt:420]
4| NSLog(@"%@ let's %@ it up in here", myString, myNumber)
output >> "Hello World let's 420 it up in here"

Importing And Exporting

Include

For C and C++ Files

Import

For Objective-C Files

Terminology

Interface

A Skeleton or Schema to create Objects or Arrays

Take the following

1| @interface Person : NSObject{
2|      NSString *name
3| }

Instance Variables: Anything inside the @interface’s {}s

To Create an Instance Method do it outside the @interface’s {}s Instance Methods use -(void):

1| @interface Person : NSObject{
2|      NSString *name
3| }
4| -(void)

Class Variables use +(void):

1| @interface Person : NSObject{
2|      NSString *name
3| }
4| +(void)

Resources

comments powered by Disqus