9.4. Introduction to Objective-C

发布时间 : 2025-10-25 13:32:30 UTC      

Page Views: 9 views

Objective C is used in the development of iOS, which is an object-oriented language, so it is very simple for programmers who have mastered the knowledge of object-oriented language.

9.4.1. Interface and implementation

The files completed in Objective are called interface files, and the definition of such files is called implementation files.

A simple interface file, MyClass.h, will look like this:

@interface MyClass:NSObject{ // 类变量声明 } // 类属性声明 // 类方法和声明 @end 

执行MyClass.m文件,如下所示

@implementation MyClass // 类方法定义 @end 

9.4.2. Create object

Finish creating the object, as shown below

MyClass *objectName = [[MyClass alloc]init] ; 

9.4.3. Method (methods)

The methods declared in Objective C are as follows:

-(returnType)methodName:(typeName) variable1 :(typeName)variable2; 

An example is shown below:

-(void)calculateAreaForRectangleWithLength:(CGfloat)length andBreadth:(CGfloat)breadth; 

You may wonder what an andBreadth string is, but its optional string can help us read and understand methods, especially when methods are called.

To call this method in the same class, we use the following statement.

[self calculateAreaForRectangleWithLength:30 andBreadth:20]; 

As mentioned above, the use of andBreath helps us understand that breath is 20. Self is used to specify that it is a class method.

类方法(class methods) You can access class methods directly without creating objects. They don’t have any variables associated with it. Examples are as follows:

+(void)simpleClassMethod; 

It can be accessed by using the class name (assumed as the MyClass class name), as follows:

[MyClass simpleClassMethod]; 

实例方法 The object of the class that can be created only accesses the instance method and the instance variable to which memory is allocated. The method of the instance is as follows:

-(void)simpleInstanceMethod; 

After creating an object of the class, it can access it. As follows:

MyClass *objectName = [[MyClass alloc]init] ; [objectName simpleInstanceMethod]; 

9.4.4. Important data types of Objective C

Serial number

Data type

1

NSString string

2

Basic types of CGfloat floating point values

3

NSInteger integer

4

BOOL Boolean type

9.4.5. Print log

NSLog is used to print a declaration that will be printed on the device log and the debug version of the console and on the separate debug mode.

Such as NSlog (@ “”)

9.4.6. Control structure

With a few additional provisions, most of the control structures are the same as those of C and C++

属性(properties)

Used to access the variable properties of the external class of the class

For example: @ property (non-atomic, strong) NSString*myString

访问属性

You can access a property using the dot operator, and to access the previous property, you can do the following

self.myString = @"Test"; 

You can also use the method of set, as follows:

[self setMyString:@"Test"]; 

类别(categories)

Class is used to add methods to an existing class. In this way, you can add methods to the class and even define the actual class in it without even executing the file. The sample category of MyClass, as follows:

@interface MyClass(customAdditions) - (void)sampleCategoryMethod; @end @implementation MyClass(categoryAdditions) -(void)sampleCategoryMethod{ NSLog(@"Just a test category"); } 

9.4.7. Array

NSMutableArray and NSArray are array classes used in ObjectiveC, the former is a mutable array, and the latter is an immutable array. It is as follows:

NSMutableArray *aMutableArray = [[NSMutableArray alloc]init]; [anArray addObject:@"firstobject"]; NSArray *aImmutableArray = [[NSArray alloc] initWithObjects:@"firstObject",nil]; 

9.4.8. Dictionaries

NSMutableDictionary and NSDictionary are dictionaries used in Objective, the former mutable dictionary and the latter immutable dictionary, as follows:

NSMutableDictionary*aMutableDictionary = [[NSMutableArray alloc]init]; [aMutableDictionary setObject:@"firstobject" forKey:@"aKey"]; NSDictionary*aImmutableDictionary= [[NSDictionary alloc]initWithObjects:[NSArray arrayWithObjects: @"firstObject",nil] forKeys:[ NSArray arrayWithObjects:@"aKey"]]; 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。