3.5. EXECUTING C CODES VIA OBJECTIVE-C 45
3.5 EXECUTING C CODES VIA OBJECTIVE-C
Now that a C code is written, it needs to be linked to the Objective-C app in order to be
executed.
In ViewController.m, just below #import "ViewController.h" , add
#import "Algorithm.h"
In the buttonPress method, include the following line:
_label.text = [NSString stringWithUTF8String:HelloWorld()];
is code line alters the text of the label in the program.
Run the program in the simulator.
On pressing the button in the simulator, the following is observed.
1. e text of the label changes.
2. In the Xcode window, Method Called gets printed in the Debug Console at the bottom.
is shows that printing can be done from the C function to the debug console in Xcode.
is feature is used for debugging purposes.
3.6 SWIFT PROGRAMMING LANGUAGE
Swift is a programming language developed by Apple as a successor to Objective-C. It provides
a modern programming-language interface with a simple syntax, making it easier to read and
maintain as compared to Objective-C. In Swift, pointers are not exposed by default to prevent
memory leakage due to improper referencing or dereferencing of memory addresses. Swift allows
less coding to be done to achieve the same objective as compared to Objective-C, e.g.:
creating a string and concatenating in Objective-C:
NSString *str = @''Hello ``;
str = [str stringByAppendingString:@''World!''];
creating a string and concatenating in Swift:
46 3. IOS SOFTWARE DEVELOPMENT TOOLS
var str = ``Hello ``
str += ``World!''
Since declarations in Objective-C are required to be done as pointers, one needs to be careful
with memory allocation and preventing memory leaks when using Objective-C. With Swift,
these issues are dealt with in an easy manner. As noted above, via type inference, Swift can
detect “Hello” as String, removing the need to explicitly state the datatype. As seen in the above
Objective-C example, there are distinct compilation units similar to C with header files to pass
variables and functions to other files. is is not required in Swift and the same is achieved by
the declaration of only one .swift file and by declaring variables and functions only once.
Swift allows calling Objective-C and C functions via a bridging header. As a result, pre-
viously developed codes can be seamlessly called in a Swift program without the need for trans-
lation. is makes Swift suitable for developing app GUIs while running processing codes in
C.
To develop a Swift project, the same Objective-C example is considered here. e follow-
ing steps need to be taken to create the same example in Swift.
Follow Section 3.1 until step 6 and set Language to Swift.
To create the layout, open ViewController.swift. Note that there is only one file associated
with the coding.
Under the class definition and before the viewDidLoad function definition, initialize a
UILabel and a UIButton via the following code. Note that one does not need to declare
them as pointers:
class ViewController: UIViewController {
var label: UILabel!
var button: UIButton!
Add the following code to the viewDidLoad function to programmatically create the UI
similar to how it is done in Objective-C. Notice that there is no underscore before label
and button . Also, memory does not need to be allocated for the labels, and functions can
be chained simply by using ‘.’ instead of ‘[]’ used in Objectove-C. e Swift code appears
below:
label = UILabel(frame:CGRect(x: 10, y: 15, width: 300, height: 30))
label.text = "Hello World"
self.view.addSubview(label)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.198.129