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
相关推荐
如果你想迅速掌握Objective-C,并有c++或Java的基础,该文档绝对是你的首选。该文档共13页,全是Objective-C知识的精华,没有在此啰嗦与其他语言相同的部分。
Objective-C基础教程.pdf(Learn Objective-C on the Mac中文版) 中文扫描版 + 英文电子书 + 源码 该资源在Mac下用BetterZIP压缩,共三个分卷,其他分卷在我的资源中找。 更新: Windows下使用7zip对part1解压...
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...
learn Objective-c on the mac 的配套代码!
关于Objective-C 编程语言的学习著作,Objective-C基础教程英文版第二版.
Objective-C基础教程源代码 Learn objective-C on the Mac
Learn Objective-C(中文版)(v2).pdfLearn Objective-C(中文版)(v2).pdfLearn Objective-C(中文版)(v2).pdfLearn Objective-C(中文版)(v2).pdf
Objective-C基础教程(Learn Objective-C)随书源码下载
Objective-C to using it confidently in your own applications. This isnɅt a rush job – so donɅt expect to just skim through the basics and be away – weɅll be going through not just the bare ...
Learn Objective-C for Java Developers Learn Objective-C for Java Developers will guide experienced Java developers into the world of Objective-C. It will show them how to take their existing language...
Objective-C基础教程.pdf(Learn Objective-C on the Mac中文版) 中文扫描版 + 英文电子书 + 源码 该资源在Mac下用BetterZIP压缩,共三个分卷,其他分卷在我的资源中找。 更新: Windows下使用7zip对part1解压...
Pro Objective-C The Objective-C ... It's also great for those developers not familiar with Objective-C that have done some C programming and also understand object-oriented programming concepts.
Amazon超级畅销书,好评如潮,全面解读Objective-C语言及Cocoa特性,用最通俗的语言阐释深奥的知识。 Objective-C是扩展C的面向对象编程语言,也是iPhone开发用到的主要语言。 本书结合理论知识与示例程序,全面...
Book Description Updated for OS X 10.9 Mavericks, iOS 7, and Xcode 5 Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming ...
Objective-C is a powerful, object-oriented extension of C, making this update the perfect follow-up to Dave Mark's bestselling Learn C on the Mac. Whether you're an experienced C programmer or you're...
Objective-C基础教程.pdf(Learn Objective-C on the Mac中文版) 中文扫描版 + 英文电子书 + 源码 该资源在Mac下用BetterZIP压缩,共三个分卷,其他分卷在我的资源中找。 更新: Windows下使用7zip对part1解压...
Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming for Apple's iOS and Mac platforms. The book makes no assumptions ...
Learn Objective-C on the Mac.pdf
Objective–C is a powerful, object–oriented extension of C, making this book the perfect follow–up to Dave Mark’s best–selling Learn C on the Mac, Mac OS X Edition. Whether you’re an experienced...