Learn Object C(1) Learn Objective-C in Day 6 - 1 ~ 3
What is Objective-C
Objective-C is a strict superset of C, we are free to use C in an Objective-C file and it will compile fine.
What will I need?
Since I installed Xcode on my MAC, I already have GCC.
Compiling your code
Open the MAC terminal and go to the work directory.
>cd /Users/carl/work/objective-c
>vi First.m
#include <stdio.h>
int main(){
printf("Hello World\n");
return 0;
}
>gcc First.m -o First
The system will compile the codes in First.m to machine codes into First.
Then I can run the codes with these commands>
>./First
I can get the println Hello World. I am a good student when I was in university while studying the C language.
That should be awesome to study objective-c from my understanding.
The Basics
int main(){
printf("Hello World\n");
return 0;
}
By convention, returning '0' signals to the calling program that our program finished execution without any errors.
Variables
int - for storing integers with no decimal point
char - for storing a character
float - for storing numbers with decimal points
double - same as float but double the accuracy
If we are not using variables, we are using constants. For instance, 123 + 2 = 125
Almost the same way of C language>
#include <stdio.h>
int main(){
int someNumber = 123;
printf("My number is %i \n", someNumber);
return 0;
}
%i integer, %f float, %e double, %c char
Conditionals
int main(){
if(1 == 1){
//...snip...
}
return 0;
}
Loops
for (x = 0; x < 9; x++){
printf("Count is: %i\n", x);
}
while ( x < 10){
x++;
printf("Count is: %i\n", x);
}
do {
x++;
printf("Count is: %i\n", x);
} while(x < 10);
Pointers
Pointers point to a location. Specifically, locations in computer memory. We have a variable named foo with value 123, we can also have a point points to foo.
int foo = 123;
int *ptr = &foo;
Wrapping Up
…snip…
Object Orientated Programming
Classes are made up of methods and attributes.
Interface and Implementation
Interface is a file that ends with a suffix of .h. Implementation is a file that ends with a suffix of .m.
Interface
@interface Car: NSObject
@property (weak, nonatomic) IBoutlet UILabel *display;
- (void) addGas;
@end
Car is extends from Object NSObject
The "-" indicates that this is an instance method, not a class method.
Implementation
#import "Car.h"
@implementation Car
- (void) addGas {
}
@end
Classes from Apple
NS is abbreviation for NextStep.
NSString immutable string
NSMutableString mutable string
NSArray immutable array
NSMutableArray mutable array
NSNumber
Pointers and Initializing
File --> New Project ---> Mac OS X--> Application ---> Command Line Tool ----> Type: Foundation
#import <Foundation/Foundation.h>
int main(int argc, constchar * argv[]) {
NSString *testString;
//pointer to NSString
testString = [[NSString alloc] init];
//have instance of NSString
testString = @"sillycat test String";
//@ a sign of NSString
NSLog(@"only string= %@",testString);
//%@ means an Objetive-C object
return 0;
}
Inheritance
NSObject ---> NSString ----> NSMutableString
---> NSArray -----> NSMutableArray
---> NSValue -----> NSNumber
References:
http://www.otierney.net/objective-c.html.zh-tw.big5
http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/_index.html
http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/chapters/Introduction.html
http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-day-1/
http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-2/
http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-day-3/
http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-day-4/
Objective C Book from Apple
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html
相关推荐
1. **消息传递**: Objective-C 是基于 Smalltalk 的,因此它的核心机制是消息传递。当你调用一个方法时,实际上是在向对象发送一条消息。例如,`[object method]` 表示向 `object` 发送名为 `method` 的消息。 2. *...
Objective-C基础教程.pdf(Learn Objective-C on the Mac中文版) 中文扫描版 + 英文电子书 + 源码 该资源在Mac下用BetterZIP压缩,共三个分卷,其他分卷在我的资源中找。 更新: Windows下使用7zip对part1解压...
1. **Objective-C语法**:Objective-C的语法基于C语言,但引入了类、接口和消息传递等概念。这包括定义类、属性(@property)和方法(@selector)的语法,以及理解类接口(.h)和实现(.m)文件的作用。 2. **对象...
you learn Objective- C in this book, you’ll be ready to dive into Cocoa with a full- blown project or another book such as Learn Cocoa on the Mac or Begin- ning iPhone Development, both by Dave Mark...
1. **Objective-C基础**:讲解Objective-C的基本语法,包括类、对象、消息传递、属性、协议等概念。 2. **内存管理**:介绍ARC(Automatic Reference Counting)自动引用计数,这是Objective-C中的内存管理机制,...
Objective-C是C语言的超集,它扩展了C语言,加入了面向对象的特性。面向对象编程(OOP)的核心概念包括类、对象、继承、封装和多态。Objective-C通过消息传递机制实现了这些概念,使得代码更加模块化和可维护。 2....
这个"Learn Objective-C 中文版 v2"的学习资源可能是针对那些希望深入理解Objective-C语言特性和编程实践的开发者设计的。下面我们将详细探讨Objective-C的关键知识点。 1. **面向对象编程基础**: - **类与对象**...
《Learn Objective-C on the Mac eBook》是一本专为Mac用户设计的深入学习Objective-C的电子书,其中包含了丰富的实例和代码示例。Objective-C是一种强大的编程语言,尤其在苹果的iOS和macOS平台上广泛使用。这本书...
1. **Objective-C简介**:介绍Objective-C的历史、特点和与其他编程语言的区别,特别是与C++和Swift的比较。 2. **基本语法**:讲解Objective-C的基础语法,如变量声明、常量定义、数据类型、流程控制(条件语句和...
Learn Objective-C(中文版)(v2).pdfLearn Objective-C(中文版)(v2).pdfLearn Objective-C(中文版)(v2).pdfLearn Objective-C(中文版)(v2).pdf
《Learn_Objective-C(zh)(v2)》是面向初学者的一份中文教程,旨在帮助读者掌握Objective-C这门编程语言,从而踏入iPhone应用开发的大门。Objective-C是由Apple公司开发的一种面向对象的编程语言,它是开发iOS和macOS...
1. **面向对象编程(Object-Oriented Programming, OOP)**:Objective-C基于C语言,并在其基础上引入了面向对象的概念,如类、对象、继承、封装和多态等。学习Objective-C首先要理解这些OOP的基本概念。 2. **类...
Objective-C支持面向对象编程的核心概念,如封装、继承和多态,它使用消息传递来调用对象的方法,消息通过方括号表示,例如[object method]。方法调用可以带有输入参数和返回值,允许方法执行所需的操作。此外,...
"Learn Objective-C(zh)(v2).pdf"这本书是一个中文版的快速入门指南,旨在帮助初学者快速掌握Objective-C的基础知识。 本书首先会介绍Objective-C的基本概念,包括类、对象、消息传递机制。类是Objective-C的核心,...