Objective-C Basic Data Type

Source: Internet
Author: User

Objective-C Basic Data Type

1. Object-C provides basic data types: int, float, double, and char.


Sample Code:

// Data type: // INTEGER (INT, short int, long int, unsigned int, unsigned short, unsigned long) nslog (@ "INTEGER :"); nslog (@ "% lu", sizeof (INT); // integer 4-byte nslog (@ "% lu", sizeof (short INT )); // short integer 2 nslog (@ "% lu", sizeof (long INT); // long integer 8 nslog (@ "% lu", sizeof (unsigned INT )); // unsigned integer 4 nslog (@ "% lu", sizeof (unsigned short); // unsigned short integer 2 nslog (@ "% lu ", sizeof (unsigned long); // unsigned long integer 8 // solid (float, double, long double) nslog (@ "solid :"); nslog (@ "% lu", sizeof (float); // floating point 4 nslog (@ "% lu", sizeof (double )); // Double Precision 8 nslog (@ "% lu", sizeof (Long Double); // long precision 16 // string char a = 'a '; char B = 100; nslog (@ "% C, % C", a, B); nslog (@ "% I, % I", a, B ); nslog (@ "I Am a string"); // string: @ "I Am a string"

Running result:

10:36:41. 618 helloworld [724: 707]Integer:

10:36:41. 624 helloworld [724: 707] 4

10:36:41. 625 helloworld [724: 707] 2

10:36:41. 626 helloworld [724: 707] 8

10:36:41. 627 helloworld [724: 707] 4

10:36:41. 628 helloworld [724: 707] 2

10:36:41. 628 helloworld [724: 707] 8

10:36:41. 629 helloworld [724: 707]Solid:

10:36:41. 630 helloworld [724: 707] 4

10:36:41. 630 helloworld [724: 707] 8

10:36:41. 631 helloworld [724: 707] 16

10:36:41. 632 helloworld [724: 707] A, D

10:36:41. 633 helloworld [724: 707] 97,100

10:36:41. 633 helloworld [724: 707]I am a string

Output Format character:

INT:% I, % d, % O, % x

Float: % F, % E, % G

Double: % F, % E, % G

CHAR: % C

Qualifier:

(Long)

Long INT: % Li

Long double: % lf

(Long)

Long long INT: % llI

(Short)

Short INT: % Hi

2. Other types:

(1) ID type

The ID type is a unique data type. In terms of concept, similar to the Java object class, it can be converted to any data type. In other words, the ID type variable can store any data type object. In terms of internal processing, this type is defined as a pointer to an object, which is actually a pointer to the instance variable of this object. Note that ID is a pointer, so you do not need to add a star number when using ID;

For example, Id Foo = nil;

This statement defines an Nil pointer, which points to any subclass of nsobject. "Id * Foo = nil" defines a pointer that points to another pointer and is directed to a subclass of nsobject;

The following is an example: two different classes (student and member) have different member variables and methods.

Student. h

#import <Foundation/Foundation.h>@interface Student : NSObject{    int sid;    NSString *name;}@property int sid;@property (nonatomic,retain)NSString *name;-(void)print;-(void)setSid:(int)sid andName:(NSString*)name;@end

Student. m

# Import "student. H "@ implementation student @ synthesize Sid, name;-(void) print {nslog (@" my student ID is: % I, my name is: % @ ", Sid, name);}-(void) setsid :( INT) sid1 andname :( nsstring *) name1 {self. SID = sid1; self. name = name1 ;}@ end

Member. h

#import <Foundation/Foundation.h>@interface Member : NSObject{    NSString *name;    int age;}@property (nonatomic,retain)NSString *name;@property int age;-(void)print;-(void)setName:(NSString*)name1 andAge:(int)age1;@end

Member. m

# Import "member. H "@ implementation member @ synthesize name, age;-(void) print {nslog (@" My name is: % @, my age is % I ", name, age);}-(void) setname :( nsstring *) name1 andage :( INT) age1 {self. name = name1; self. age = age1 ;}@ end

In Main. M:

Id fo; nslog (@ "% lu", sizeof (FO); member * member1 = [[member alloc] init]; [member1 setname: @ "Sam" andage: 36]; ID data; data = member1; // because of the general nature of the ID type, we can assign the created object to data; [data print]; student * student1 = [[STUDENT alloc] init]; [student1 setsid: 1122334455 andname: @ "Lee"]; Data = student1; [data print];

Running result:

14:41:01. 768 helloworld [440: 707] 8

14:41:01. 769 helloworld [440: 707]My name is:Sam,My age is36

14:41:01. 769 helloworld [440: 707]My student ID is1122334455,My name is:Lee

Note: The ID type can be converted to a specific object by using a type conversion character.
See the above program with @ property and @ synthesize modifier;

@ Property and @ synthesize: Use @ property in the interface file to identify the attribute (generally an instance variable). In the implementation file, use @ synthesize to identify the declared attribute, the system automatically generates the setting and obtaining methods.

Format: @ property (attribute list) instance variable,


(2) enumeration data types, typedef syntax, conversion of data types, and C
Yes.

(3) Boolean bool values:

The Boolean value is YES/NO, or 1/0. False is 0. There is nothing to say.

(4) SEL Selector

A selector is a pointer to a method. It can be simply understood that the specified method will be executed when the program runs here;

(5) nil and nil

Nil is the same as null in C. Nil indicates an Objctive-C object. The pointer of this object points to null (nothing is null ).

Nil is different from nil in the upper-case nil. Nil defines a class (class rather than an object) that points to null ).

(6) imp

IMP is a pointer to a function. The directed function includes the ID ("self" pointer), The called sel (method name), and some other parameters.

To put it bluntly, IMP is the implementation method.

(6) Class

Similar to Java, you can use the class to obtain the class to which an object belongs.

(7)Method

Method (the method we often call) represents a type, which is related to selector and implementation (implementation.

OK!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.