父:你去買汽水。

子:是可樂還是雪碧?

父:可樂。

子:鐵罐還是瓶裝?

父:瓶裝。

戮克 發表在 痞客邦 留言(0) 人氣()

來源:http://blog.diveinedu.net/objective-c不和c那样有函数重载/

今天有学员问在看Programming iOS 6的书上看到OC里不能像C++那样形式做函数重载的问题.

No Overloading

The data type returned by a method, together with the data types of each of its param‐ eters in order, constitute that method’s signature. It is illegal for two methods of the same type (class method or instance method) to exist in the same class with the same name but different signatures.

So, for example, you could not have two MyClass instance methods called myMethod, one of which returns void and one of which returns an NSString. Similarly, you could not have two MyClass instance methods called myMethod:, both returning void, one taking a CGFloat parameter and one taking an NSString parameter. An attempt to vi‐ olate this rule will be stopped dead in its tracks by the compiler, which will announce a “duplicate declaration” error. The reason for this rule is that if two such conflicting methods were allowed to exist, there would be no way to determine from a method call to one of them which method was being called.

You might think that the issue could be decided by looking at the types involved in the call. If one myMethod: takes a CGFloat parameter and the other myMethod: takes an NSString parameter, you might think that when myMethod: is called, Objective-C could look at the actual argument and realize that the former method is meant if the argument is a CGFloat and the latter if the argument is an NSString. But Objective-C doesn’t work that way. There are languages that permit this feature, called overloading, but Objective- C is not one of them. 

我在这阐述一下这2个OO语言之间的一些异同:

1, 两者最大的相同便是: 都是从 C 演化而来的面向对象语言, 两者都兼容标准 C 语言.

2, 两者最大的不同便是: Objective C 是完全动态(运行时绑定)的,而 C++是部分动态的(编译时绑定和运行时绑定)。

Objective C 支持在运行时动态类型决议(dynamic typing),动态绑定(dynamic binding)以及动态装载(dynamic loading);

戮克 發表在 痞客邦 留言(0) 人氣()

來源:http://nscoder.me

 

还记得以前在网页上用 iframe 指定一个 URL 并通过 native 的 - webView:shouldStartLoadWithRequest:navigationType: 回调方法拦截请求,再蛋疼的各种截取和判断字符串,结合 json 结构的字符串来获取 web 端发来的数据。
通过 - stringByEvaluatingJavaScriptFromString: 来拼接老长的字符串来传递数据给 web 端吗。。。

现在我们可以对这种方式说 「爱过~」 了。

 

先看一下 Objective-C 与 JavaScript 之间类型的对应关系

Objective-C type  JavaScript type 
nil  undefined
NSNull  null
NSString  string
NSNumber  number, boolean
NSDictionary  Object object
NSArray  Array object
NSDate  Date object
NSBlock  Function object
id  Wrapper object
Class  Constructor object 

 

返回的 JSValue 转换为 Objective-C 类型的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (BOOL)toBool;
- (double)toDouble;
- (int32_t)toInt32;
- (uint32_t)toUInt32;
- (NSNumber *)toNumber;
- (NSString *)toString;
- (NSDate *)toDate;
- (NSArray *)toArray;
- (NSDictionary *)toDictionary;
- (id)toObject;
- (id)toObjectOfClass:(Class)expectedClass;

 

我们主要使用的是 JSContext 这个类,下面通过几个简单的例子来演示。

首先需要加入 JavaScriptCore.framework 并引入。

1
#import <JavaScriptCore/JavaScriptCore.h>

Objective-C 调用纯 JavaScript

例如有一段计算阶乘的脚本

戮克 發表在 痞客邦 留言(0) 人氣()

IOS8获取当前经纬度、地理位置
/*集成说明:
1、在plist添加
NSLocationAlwaysUsageDescription = YES
NSLocationWhenInUseUsageDescription = YES
2、导入CCLocationManager.h头文件
3、通过block回调获取经纬度、地理位置等
*/

程式碼載點http://code.cocoachina.com/detail/280904


文章標籤

戮克 發表在 痞客邦 留言(0) 人氣()

Error in registration. Error: Error Domain=NSCocoaErrorDomain Code=3000 "找不到應用程式的有效「aps-environment」授權字串" UserInfo=0x170270300 {NSLocalizedDescription=找不到應用程式的有效「aps-environment」授權字串}

因為應用程式沒有聲請憑證所以無法產生裝置碼

請繳費然後去開發者的網頁聲請

戮克 發表在 痞客邦 留言(0) 人氣()

     今天跟爸媽去苗栗老爸迷路就跑到後龍看風車了。風景不錯人潮又少超級適合親近大自然的。很多風車有種童話裡的感覺!缺點就是台灣人很缺德海邊垃圾很多。附上地圖有興趣的人可以去...順便一提IPHONE6照相真得超好,隨便照都能看。陰天的小小缺點是飄雨...

以下為照片


戮克 發表在 痞客邦 留言(0) 人氣()