Objective-C: NSInputStream and NSOutputStream for testing purposes - objective-c

I have an class in my application that is initialized with an NSInputStream. For testing purposes i want to write data on an NSOutputStream which then is received by that input stream.
This should trigger the NSStreamEventHasBytesAvailable event.
The only thing is that i do not know how to set this up. Does anybody has an idea? Or suggestions how to unit test a class with has an NSStream as dependency.
Thanks

This is actually not very tricky, I've described how to do it using the SimpleURLConnection sample code available on the apple developer site.
This is the question and my answer there will work here too.

This is actually really tricky since it appears that you can only create an input stream using a file, a URL or an NSData. You can't connect an input stream to an output stream, although you might be able to write a single class that implements both interfaces.
The easiest way to do the unit test would probably be to create your input stream from an NSData whose bytes you have already read in from the output stream.

Related

Pretty Print object in Swift on debug area

I am new to swift. I am trying to find a way to accomplish that.
In objective-c project, when I NSLog a response body from api, I got something like this. its pretty and readable. Ojective-c Debug area
However, when i use swift, when I print a response body from api, I got something like that. It's super hard to read. Swift Debug area
is there any way in swift, i could see the same format in debug area same as objective-c project.
If anyone could help, I really appreciate.
Thanks in advance.
It mainly depends on what you're trying to print in the Debug area. From Apple docs:
Swift provides a default debugging textual representation for any type. That default representation is used by the String(reflecting:) initializer and the debugPrint(_:) function for types that don’t provide their own.
From the image you've linked, it looks like you're printing a Dictionary, which prints out like that and there's not a straightforward way to change it. Considering you're getting data from an API, you can do debugging in three ways:
Print the raw body: if your server is returning JSON, that's usually readable and, in case it isn't, there are many JSON formatters/viewers online.
Parse the response into instances of a class that you define as conforming to CustomDebugStringConvertible; then, print the resulting array. This will not work in case the server response is malformed.
Loop through the dictionary and manually print keys and values in a format that looks readable to you.

convert an object to JSON in objective c

I work in tool theos projects and i want convert an object into JSON.
I need a easy to use library with examples for converting NSObjects to JSON and back again
I check a lot of question like this but i can't use them.
I use JSONModel library but i have a lot of errors.
Anybody body have a good tutorial or a sample code to convert NSObject to JSON?
I don't have any idea whether I can created a json or not.
How can I fix this?
Look at my library - https://github.com/DimasSup/BaseMangedObjectModel
With it you can serialize/deserialize any your class. aslo can save it to SQLite database if needed. Also there are NetworkHelper class which help you send/receive your classes from remote server.

Mock CLLocationManager (swift) for Testing

Question 1
Before I actually start writing something myself are there existing frameworks (either obj-c or swift) that wrap CLLocationManager into a mocking framework?
Question 2
What is the best way to go about making a mock Location Manager?
Create a class that wraps around CLLocationManager, any calls into that class will return test data or actual CLLocation data depending on what "mode" we are running in
Extend CLLocationManager and override all the calls I want to "muck" with
Is there a 3rd option I haven't thought of?
Mostly I'm wondering what the pro/con of each approach would be here assuming there isn't anything already available
[edit]
Although there is a simulate location feature in XCode it does not give me exactly what I need. If I remember correctly it does not do headings, they have to be interpolated. Furthermore I may ultimately modify this class to drive CLLocationManager from a flight simulator
So I thought I'd revisit this question. What I ended up doing was writing an entire framework to handle Location (and other data) for the app in question. We are using external GPS sensors.
The class is called DataManager and I created a generalized LocationMessage type (which can be converted into CLLocation with msg.asCLLocation).
Part of this mega framework allows for recording of data and file playback, we can drive positions from
CoreLocation
An Xplane plugin
UDP Streams
Recorded Files
External GPS Sensors
So basically the answer is:
Create a class that wraps around CLLocationManager, any calls into that class will return test data or actual CLLocation data depending on what "mode" we are running in

Parsing the Xml data in xcode

I am beginner to iphone. My requirement is to invoke the sap webservice in iphone.I got the result data which is in xml form.The result is to be in NSString which is of Xml form.Then how to get that result into the array in xcode.Please help me.
Here is a great library on GitHub-XML to NSDictionary
It isn't quite an NSArray but xml files are rarely just Arrays, so this provides an intermediate, dictionaries when needed, arrays if possible.
1. You can use NSXMLParser for this purpose, read through the delegate methods in https://developer.apple.com/library/ios/documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html
2. This question is pretty repetitive, you could have just googled "Parsing the Xml data in iPhone" or "Parsing the Xml data using xCode" you would have got loads of results. Go trhough this ->
http://www.xcode-tutorials.com/parsing-xml-files/
and maybe this-> http://www.edumobile.org/iphone/iphone-programming-tutorials/parsing-an-xml-file/
3. Next time please do search.
Your question is quite general and it's difficult to give you a specific answer.
Here some guidelines to consume web services in iOS.
First, you need to have a mechanism that helps you to consume the ws. To do it, you can use NSUrlConnection class (see NSURLConnection Class) or use a the ASIHttpRequest framework (see ASIHTTPRequest). In this manner you can make requests and dowload response messages.
Since you use soap messages, you have first to create the request message manually. You can use class method of NSString stringWithFormat or use ASIFormDataRequest class of ASIHttpRequest framework.
Finally, if you receive a message that is like the following you posted in your comment, you need to parse it. Remember that is a soap message and it doesn't have only your tags. To do it, you can use NSXMLParser class (see NSXMLParser Class) or use GDataXML parser (see how-to-read-and-write-xml-documents-with-gdataxml).
Out there there are plenty of tutorials or posts on how to "consume web services on iOS". You can find them. In addition, there are also some kits that, taken the your service, create class wrappers to consume your services. In this case you don't need to create or parse manually masseges.
A last remark. When you need to consume data taken from a service, maybe a REST architecture could be easier to set up.
Hope it helps.

Creating an Objective-C API

I have never made an API in objective-c, and need to do this now.
The "idea" is that I build an API which can be implemented into other applications. Much like Flurry, only for other purposes.
When starting the API, an username, password and mode should be entered. The mode should either be LIVE or BETA (I guess this should be an NSString(?)), then afterwards is should be fine with [MyAPI doSomething:withThisObject]; ect.
So to start it [MyAPI username:#"Username" password:#"Password" mode:#"BETA"];
Can anyone help me out with some tutorials and pointer on how to learn this best?
It sounds like what you want to do is build a static library. This is a compiled .a file containing object code that you'll distribute to a client along with a header file containing the interface. This post is a little outdated but has some good starting points. Or, if you don't mind giving away your source code, you could just deliver a collection of source files to your client.
In terms of developing the API itself, it should be very similar to the way you'd design interfaces and implementations of Objective-C objects in your own apps. You'll have a MyAPI class with functions for initialization, destruction, and all the functionality you want. You could also have multiple classes with different functionality if the interface is complex. Because you've capitalized MyAPI in your code snippet, it looks like you want to use it by calling the class rather than an instance of the class - which is a great strategy if you think you'll only ever need one instance. To accomplish this you can use the singleton pattern.
Because you've used a username and password, I imagine your API will interface with the web internally. I've found parsing JSON to be very straightforward in Objective-C - it's easy to send requests and get information from a server.
Personally I would use an enum of unsigned ints rather than a NSString just because it simplifies comparisons and such. So you could do something like:
enum {
MYAPI_MODE_BETA,
MYAPI_MODE_LIVE,
NUM_MYAPI_MODES
};
And then call:
[MyAPI username:#"Username" password:#"Password" mode:MYAPI_MODE_BETA];
Also makes it easy to check if they've supplied a valid mode. (Must be less than NUM_MYAPI_MODES.)
Good luck!