2

我已经开发了一个支持 iOS 4.3+ 的应用程序。我想从ASIHTTPRequest转移 到 AFNetworking 但在文档上说我应该使用 0.10.x 模块而不是当前模块。https://github.com/AFNetworking/AFNetworking#requirements 使用AFNetworking的当前代码(1.x)的问题是(https://github.com/AFNetworking/AFNetworking/issues/545):

  1. ARC 1.x 中的介绍
  2. NSJSONSerialization的使用
  3. imp_implementationWithBlock() API 区别

但是 iOS 4.3 支持 ARClite,并且通过 1.x 的代码,我没有遇到任何使用可能导致问题的弱引用的属性。http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/ObjCAvailabilityIndex/_index.html iOS 4.3 支持imp_implementationWithBlock() 只有两个引用使用 NSJSONSerialization,我将它们更改为 JSONKit 调用->

diff --git a/AFNetworking/AFHTTPClient.m b/AFNetworking/AFHTTPClient.m
index 62fc30a..3a60da5 100755
--- a/AFNetworking/AFHTTPClient.m
+++ b/AFNetworking/AFHTTPClient.m
@@ -24,6 +24,7 @@

 #import "AFHTTPClient.h"
 #import "AFHTTPRequestOperation.h"
+#import "JSONKit.h"

 #import <Availability.h>

@@ -163,7 +164,7 @@ - (NSString *)URLEncodedStringValueWithEncoding:(NSStringEncoding)stringEncoding

 static NSString * AFJSONStringFromParameters(NSDictionary *parameters) {
     NSError *error = nil;
-    NSData *JSONData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error];;
+    NSData *JSONData = [parameters JSONDataWithOptions:JKSerializeOptionNone error:&error];

     if (!error) {
         return [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
diff --git a/AFNetworking/AFJSONRequestOperation.m b/AFNetworking/AFJSONRequestOperation.m
index 607f247..c7367dc 100755
--- a/AFNetworking/AFJSONRequestOperation.m
+++ b/AFNetworking/AFJSONRequestOperation.m
@@ -21,6 +21,7 @@
 // THE SOFTWARE.

 #import "AFJSONRequestOperation.h"
+#import "JSONKit.h"

 static dispatch_queue_t af_json_request_operation_processing_queue;
 static dispatch_queue_t json_request_operation_processing_queue() {
@@ -66,7 +67,7 @@ - (id)responseJSON {
         if ([self.responseData length] == 0) {
             self.responseJSON = nil;
         } else {
-            self.responseJSON = [NSJSONSerialization JSONObjectWithData:self.responseData options:0 error:&error];
+          self.responseJSON = [self.responseData objectFromJSONDataWithParseOptions:JKSerializeOptionNone error:&error];
         }

         self.JSONError = error;

虽然这编译得很漂亮,但我仍然对这样做持怀疑态度。这是正确的方法吗?

4

1 回答 1

1

您可以更改一些内容以使 AFNetworking >= 1.0 以在 iOS < 5 上编译,但不建议这样做,因为后续版本中可能会发生其他重大更改。

但是,如果对创建一个无法包含主线错误修复和功能的分支感到满意,那么您的方法本质上没有任何问题。

于 2013-05-24T20:44:24.703 回答