which JSON framework with ios SDK? - ios

There are couple of JSON frameworks. JSONKit , SBJSON , Native JSON and other. Which one is the best to use for Iphone app OR ios App and why ?

I have used SBJson for two years, and many Object-C API use SBJson too.
It is quite hard to compare different json apis, however SBJson is easy to use and widely used.

if you want to target just iOS5 and above you could try NSJSONSerialization
Basically iOS 5 has a new class named NSJSONSerialization. It has a
static method called JSONObjectWithData:options:error that takes an
NSData and gives you back a Foundation object – usually an
NSDictionary or an NSArray depending what do you have at the top of
your JSON file hierarchy.
an excellent example
from Ray Wenderlich

Related

Pure Swift project. Which Apple APIs am I allowed to use?

Every since Apple started open-sourcing Swift last year, I have wondering about which frameworks are going to be distributed along with its standard library.
I'm working on a pure Swift project at the moment and need to use some common objects without compromising compatibility with other (POSIX) systems. Are these objects shipped in Swift's STL?
(De)serialization objects: NSCoder, NSKeyedArchiver, NSCoding, NSXMLParser
Concurrency objects: NSOperationQueue, dispatch_async(), dispatch_sync()
File manipulation objects: NSFileManager, NSURL, NSData
And if not, are there any pure Swift alternatives?

How to deal with the lack of reflection in Swift?

As an experienced Objective-C developer who is now learning Swift , I'm really missing some of the reflection and dynamic features of Objective-C.
For eg: I had written a JSON serializer which automatically mapped keys and values using KVO and Objective C introspection , and there are open source libraries like Mantle which do this.
I could declare my object as an NSObject subclass and proceed but I feel that this is not the Swift way of doing things.
Is there any other way to accomplish the same tasks , while avoiding boilerplate , using what Swift provides ?
EDIT: (2016) this answer is auto-dated. Some of the advice may still be relevant but now that Swift is open-source, I would look into other possible answers.
There is no native KVO reflection like what you described built into Swift. See:
https://stackoverflow.com/a/24092370/798682
And based on what we do know about how the Swift compiler optimizes method execution at compile time (vs the pure runtime implementation of ObjC) it doesn’t seem likely to be added anytime soon. See https://stackoverflow.com/a/25438299/798682 and
http://blog.untitledkingdom.co.uk/obj-c-vs-swift/
for more info on that.
With all that being said, here is a blog post on some KVO alternatives in Swift:
http://blog.scottlogic.com/2015/02/11/swift-kvo-alternatives.html
and another that details some of the reflection capabilities that are in Swift:
http://freecake.angelodipaolo.org/simple-reflection-in-swift/.

Why do we still use NSDictionary and NSArrays in swift ios applications?

I see a lot of code for swift ios tutorials on the internet still using the old objective-c data types like NSDictionary and NSArrays instead of using the modern Array and Dictionary data types that come with Swift.
Most often I see a lot of type casting going on using as? etc. I have seen this mostly in examples involving json parsing from a webservice.
So why is this? Is this because swift isn't fully compatible withe cocoa touch apis yet? Or is there another reason like performance?
Even though you used the NSArray and NSDictionary, swift will automatically bridges to their native swift equivalent Array and Dictionary.
By using the Swift native, the performance will be great.
Please refer the documentation
Because you are calling code in the Cocoa library that uses NSDictionary and NSArray and Apple isn't going to rewrite all these libraries.
Your question whether Swift is not fully compatible with Cocoa APIs is nonsensical. It is compatible, or you couldn't call Cocoa. But the Cocoa APIs are not changing. Cocoa is a library; it uses and supports the classes that it uses and supports, and using a different programming language isn't going to change that.

ios - NSJSONSerialization to save json to ipad

Sorry that my question is not as clear as I thought:
Is it possible to use NSJSONSerialization to save json data as json file specifically into iPad? I am developing for ios6.
I understand that you can convert it into NSDictionary and NSArray then save it into plist. But what I am looking for is to have a json file in your ios device, that allow to read and write.
Is that possible? Thanks!
Yes, you can use NSJSONSerialization to convert standard collection (dictionary/array) to a JSON format in NSData, which you can then save to persistent storage.
But, is there some reason you want to save your data as JSON rather than the myriad of native formats that Apple provides? JSON introduces a few limitations and requires an extra step to save it, so generally you wouldn't use it for local persistent storage. It's become a bit of a lingua franca (much as XML is) for communicating between between systems, but it wouldn't generally be one's first choice for local persistent storage.
There are a variety of different Cocoa Touch technologies designed for persistence.
See the relevant guide for each of the relevant technologies:
Preferences and Settings Programming Guide - You can use NSUserDefaults to save basic user preferences and settings
Property List Programming Guide - The most primitive and easy way to save arrays/dictionaries containing standard Cocoa objects (strings, numbers, etc.). E.g.
// to save dictionary to plist
[dictionary writeToFile:path atomically:YES];
or
// to load dictionary from plist
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
Archives and Serializations Programming Guide - A variant on the property list approach, which allows you to modify your custom classes so that they can be archived (saved) with NSKeyedArchiver and unarchived (loaded) with NSKeyedUnarchiver.
Core Data Programming Guide - Apple's robust object persistence technology framework.
SQLite - See Ray Wenderlich tutorial or the SQLite.org site.
For more information, see the relevant guide.

Combination of URLRequest +JSON Parsing

What would be the best option for:
URL Request
NSURLConnection
ASIHTTPRequest
AFNetwork
JSON Parsing
NSJSONSerialization
SBJSON
I have picked ASIHTTPRequest+NSJSONSerialization, but I am not quite sure! Any idea(s) or suggestion(s)
Features wise, AFNetworking > ASIHTTPRequest > NSURLConnection. ASIHTTPRequest is not supported anymore. It's a closed project. NSURLConnection is apple-provided so it's barebones. AFNetworking is still an active project and from the talk of my colleagues here in SO, it's getting better.
NSJSONSerialization is Apple-provided and SBJSON is third party again.SBJSON gives your a better/easier interface to convert data objects in either formats (Foundation <--> JSON).
BUT - things majorly depend on what you want to get out of your project and what the requirements are. Each of these projects has it's advantages and disadvantages. You just need to research each of these frameworks and choose the best that suits your project. Also, there are tons of resources here on SO that help you decide what to pick.

Resources