object -c OOP , 源码组织 ,Foundation 框架 详解1

?object -c? OOP ,??源码组织??,Foundation?框架?详解1

1.1 So what is OOP? OOP is a way of constructing software composed of objects. Objects are like little machines living inside your computer and talking to each other to get work done.

oop?就是由对象构成的软件。 对象就像一些小的机器存在在你的电脑中,通过相互对话来进行工作。?

?

1.2 Source File organization?

If you use .mm for the file extension, you‘re telling the compiler you‘ve written your code in Objective-C++, which lets you use C++ and Objective-C together.

如果你用.mm文件,那么你在告诉编译器你在用oc++?在写代码。

?

1.3?

?1.3.1? a quick tour of the foundation kit?

Foundation framework has a bunch of useful low-level, data-oriented classes and types. We‘ll be visiting a number of these, such as NSString, NSArray, NSEnumerator, and NSNumber.

基础框架 有一系列低层次,面向数据的类和类型。

?

Foundation framework is built on top of another framework called CoreFoundation.

基础框架建立在核心框架之上。

If you come across function names or variable names that start with "CF," they are part of CoreFoundation.

Most of them have equivalents in Foundation framework, and some of them can be easily converted from one to the other.

?

1.3.2 ? Some useful types?

home on the range?

?

typedef struct _NSRange

{

?unsigned int location;

?unsigned int length;

} NSRange;

?

First, you can assign the field values directly:

NSRange range;

range.location = 17;

range.length = 4;

Second, you can use the C aggregate structure assignment mechanism (doesn‘t that sound impressive?):

NSRange range = { 17, 4 };

Finally, Cocoa provides a convenience function called NSMakeRange():

NSRange range = NSMakeRange (17, 4);

1.3.3 Geometric types?

You‘ll often see types that deal with geometry and have the prefix "CG," such as CGPoint and CGSize. These types are provided by the Core Graphics framework, used for 2D rendering.

CG prefix .?这些类型由 核心图形库框架提供。 用作2d?展示。

?

CGPoint represents an (x, y) point in the Cartesian plane:笛卡尔坐标系坐标

struct CGPoint

{

float x;

float y; };

CGSize holds a width and a height:

宽和高

struct CGSize

{

float width;

?float height;

};

?

Cocoa provides a rectangle type, which is a composition of a point and a size:

struct CGRect

{

?CGPoint origin;

?CGSize size;

};

Cocoa gives us convenience functions for making these bad boys too: CGPointMake(), CGSizeMake(), and CGRectMake().

?

1.3.4 String us along?

Cocoa‘s NSString has a bunch of built-in methods that make string handling much easier.

NSString?有许多内在方法让我们处理字符串更容易。

?

1.3.4.1Build That String?

NSString‘s stringWithFormat: method creates a new NSString just like that, with a format and arguments:

When you declare a method with the plus sign, you‘ve marked the method as a class method.

当你声明方法前有+号时, 你标注了该方法为类方法。

Class methods used to create new objects are called factory methods.

类方法通常用来创建新对象,被称作工厂方法。

?

1.3.4.2 Size matters?长度问题

Another handy NSString method (an instance method) is length, which returns the number of characters in the string:

-(NSUInteger)length;

?

NSUInteger length=[height length];

?

1.3.4.3 Comparative Politics??比较策略

isEqualToString: compares the receiver (the object that the message is being sent to) with a string that‘s passed in as an argument. isEqualToString: returns a BOOL (YES or NO) indicating if the two strings have the same contents. It‘s declared like this:

- (BOOL) isEqualToString: (NSString *) aString;

To compare strings, use the compare: method, which is declared as follows: - (NSComparisonResult) compare: (NSString *) aString;

[@"aardvark" compare: @"zygote"] would return NSOrderedAscending.

?

1.3.4.4Insensitivity Training

compare: does a case-sensitive comparison. In other words, @"Bork" and @"bork", when compared, won‘t return NSOrderedSame. There‘s another method, compare:options:, that gives you more control:

- (NSComparisonResult) compare: (NSString *) aString

? ? options: (NSStringCompareOptions) mask;

For example, if you want to perform a comparison ignoring case but ordering numbers correctly, you would do this:

if ([thing1 compare: thing2 options: NSCaseInsensitiveSearch | NSNumericSearch]

? ? == NSOrderedSame)

{

? NSLog (@"They match!");

}

?

1.3.4.5 Is it inside ??在里面吗

the first checks whether a string starts with another string, and the second determines if a string ends with another string:

- (BOOL) hasPrefix: (NSString *) aString;

- (BOOL) hasSuffix: (NSString *) aString;

And you‘d use these methods as follows:

NSString *fileName = @"draft-chapter.pages";

if ([fileName hasPrefix: @"draft"])

{

? // this is a draft

}

if ([fileName hasSuffix: @".mov"])

{

? // this is a movie

}

If you want to see if a string is somewhere inside another string, use rangeOfString: - (NSRange) rangeOfString: (NSString *) aString;

NSRange range = [fileName rangeOfString: @"chapter"];

1.3.4.6 Mutability

NSStrings are immutable.Cocoa provides a subclass of NSString called NSMutableString. Use that if you want to slice and dice a string in place.

You can create a new NSMutableString by using the class method stringWithCapacity:, which is declared like so: ?

可以用类方法?创建NSMutableString

+ (id) stringWithCapacity: (NSUInteger) capacity;

The capacity is just a suggestion to NSMutableString, like when you tell a teenager what time to be home.

这个容量只是个建议,就像你告诉年轻人什么时候回家。

Once you have a mutable string, you can do all sorts of wacky tricks with it. A common operation is to append a new string, using appendString: or appendFormat:, like this:

- (void) appendString: (NSString *) aString;

- (void) appendFormat: (NSString *) format, ...;

NSMutableString *string = [NSMutableString stringWithCapacity:50];

[string appendString: @"Hello there "];

[string appendFormat: @"human %d!", 39];

You can remove characters from the string with the deleteCharactersInRange: method:

?- (void) deleteCharactersInRange: (NSRange) aRange;

?

NSMutableString *friends = [NSMutableString stringWithCapacity:50];

[friends appendString: @"James BethLynn Jack Evan"];

NSRange jackRange = [friends rangeOfString: @"Jack"];

jackRange.length++; // eat the space that follows

[friends deleteCharactersInRange: jackRange];

?

?

?

?

?

?

时间: 2024-08-05 14:51:17

object -c OOP , 源码组织 ,Foundation 框架 详解1的相关文章

Objective - c Foundation 框架详解2

Objective - c  Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such as NSArray and NSDictionary whose instances exist just to hold onto other objects. cocoa 提供了一系列的集合类,例如,NSarray,NSdictionary.它们存在的目的就是为了保持其他对象. 1.1.1N

spring 源码解读与设计详解:3 FactoryBean

上一篇文章讲到BeanFactory,BeanFactory是实现spring IOC原理实现的根接口,而本篇所讲的FactoryBean则是AOP原理实现的重要接口. 1. 先看FactoryBean的源码: public interface FactoryBean<T> { T getObject() throws Exception; Class<?> getObjectType(); boolean isSingleton(); } 2. 下面简单讲一下FactoryBea

spring 源码解读与设计详解:2 BeanFactory

在spring的官网中我们看到,spring的产品已经发展的非常壮大,然而很多产品对于很多公司来讲用的非常少,甚至用不到.因此本系列的源码解读也不会涉及全部的spring的产品.而是只对spring的核心功能IoC和AOP进行解释. 所谓源码解读,解读的是什么?实际上源码解读读的更多的是源码的注释,因为一个类的作用.一个接口或者一个方法的作用,我们往往是要根据注释才知道,这也是为什么在代码规范中,注释是一个非常重要的模块的原因. 参考: Spring源码分析--BeanFactory体系之接口详

vuex 源码解析(四) mutation 详解

mutation是更改Vuex的store中的状态的唯一方法,mutation类似于事件注册,每个mutation都可以带两个参数,如下: state ;当前命名空间对应的state payload   ;传入的参数,一般是一个对象 创建Vuex.Store()仓库实例时可以通过mutations创建每个mutation 我们不能直接调用一个mutation,而是通过 store.commit来调用,commit可以带两个参数,如下: type ;对应的mutation名 payload ;传入

Spark Streaming源码解读之Job详解

一:Spark Streaming Job生成深度思考 1. 做大数据例如Hadoop,Spark等,如果不是流处理的话,一般会有定时任务.例如10分钟触发一次,1个小时触发一次,这就是做流处理的感觉,一切不是流处理,或者与流处理无关的数据都将是没有价值的数据,以前做批处理的时候其实也是隐形的在做流处理. 2. JobGenerator构造的时候有一个核心的参数是jobScheduler, jobScheduler是整个作业的生成和提交给集群的核心,JobGenerator会基于DStream生

CentOS6系统源码安装LNMP环境详解

一.安装nginx 以下命令均在root权限下执行,普通用户可通过su命令切换1.安装依赖 yum install gcc-c++ yum install pcre pcre-devel yum install openssl openssl-devel 2.下载源码 wget http://nginx.org/download/nginx-1.8.1.tar.gztar -zxvf nginx-1.8.1.tar.gzcd nginx-1.8.1 3.创建nginx用户 useradd -M 

CentOS7最小化源码安装LAMP-步骤详解

系统:CentOS 7.3.1611(最小化安装) 软件:httpd-2.4.27 mysql-5.7.18 php-5.6.3 一.配置系统环境 1.1. 查看系统版本 # cat /etc/centos-release CentOS Linux release 7.3.1611 (Core) 1.2. 查看防火墙状态,关闭防火墙及其开机启动 # systemctl status firewalld # systemctl stop firewalld # systemctl disable

linux下源码编译安装mysql详解

1.redhat5环境下,首先安装编译环境 yum groupinstall -y  "Development Libraries"   "Development Tools" 2.由于源码编译mysql需要cmake命令,所以先要编译安装cmake包 首先下载cmake包,这里下载使用cmake-2.8.8.tar.gz tar xf cmake-2.8.8.tar.gz cd cmake-2.8.8 ./configure make && mak

spark core源码分析15 Shuffle详解-写流程

博客地址: http://blog.csdn.net/yueqian_zhu/ Shuffle是一个比较复杂的过程,有必要详细剖析一下内部写的逻辑 ShuffleManager分为SortShuffleManager和HashShuffleManager 一.SortShuffleManager 每个ShuffleMapTask不会为每个Reducer生成一个单独的文件:相反,它会将所有的结果写到一个本地文件里,同时会生成一个index文件,Reducer可以通过这个index文件取得它需要处理