Pretty Print object in Swift on debug area - objective-c

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.

Related

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.

Endeca Assembler: Customize response header

I want to insert a new (key -> value) pair in response header from Endeca Assembler. Is it possible to do this?
Thanks
First of all, I want to clarify some things because the terminology about Assembler can get a little confusing. I'm not sure how you have designed your program, but just keep in mind that Assembler is just a Java API, so it's kind of unclear to say something like "response header from Endeca Assembler". That statement seems to imply that Assembler is a webservice, but it isn't. In my experience, people commonly mistakenly refer to the discover-data (Discover service) example app as "Assembler" or "Assembler Service", but it really isn't a general-purpose webservice; it's designed as a reference application to be used specifically with the Discover dataset (But people still use discover-data as a starting point for building production-facing applications). So, bear in mind that I'm not exactly sure what you are referring to.
Anyway, somewhere in your code, you should have a call of something like "contentItem.assemble()", which runs your cartridge handlers on that content item and returns an object of type ContentItem. In the Discover webapp, it then serializes this content item to JSON or XML or renders a JSP page (depending on the request parameters). I assume your application does something similar.
It's a simple matter of adding properties to ContentItem, because ContentItem implements map. So, you can do something like this:
ContentItem responseContentItem = contentItem.assemble();
responseContentItem.put("myKey","myValue");
...continue by serializing responseContentItem or whatever you want to do with it
Do like this:
responseContentItem.put("key", "value");
as the resposne from Endeca Assembler is simply a Map.

How to statically dump all ObjC methods called in a Cocoa App?

Assume I have a Cocoa-based Mac or iOS app. I'd like to run a static analyzer on my app's source code or my app's binary to retrieve a list of all Objective-C methods called therein. Is there a tool that can do this?
A few points:
I am looking for a static solution. I am not looking for a dynamic solution.
Something which can be run against either a binary or source code is acceptable.
Ideally the output would just be a massive de-duped list of Objective-C methods like:
…
-[MyClass foo]
…
+[NSMutableString stringWithCapacity:]
…
-[NSString length]
…
(If it's not de-duped that's cool)
If other types of symbols (C functions, static vars, etc) are present, that is fine.
I'm familiar with class-dump, but AFAIK, it dumps the declared Classes in your binary, not the called methods in your binary. That's not what I'm looking for. If I am wrong, and you can do this with class-dump, please correct me.
I'm not entirely sure this is feasible. So if it's not, that's a good answer too. :)
The closest I'm aware of is otx, which is a wrapper around otool and can reconstruct the selectors at objc_msgSend() call sites.
http://otx.osxninja.com/
If you are asking for finding a COMPLETE list of all methods called then this is impossible, both statically and dynamically. The reason is that methods may be called in a variety of ways and even be dynamically and programmatically assembled.
In addition to regular method invocations using the Objective-C messages like [Object message] you can also dispatch messages using the C-API functions from objc/message.h, e.g. objc_msgSend(str, del). Or you can dispatch them using the NSInvocation API or with performSelector:withObject: (and similar methods), see the examples here. The selectors used in all these cases can be static strings or they can even be constructed programmatically from strings, using things like NSSelectorFromString.
To make matters worse Objective-C even supports dynamic message resolution which allows an object to respond to messages that do not correspond to methods at all!
If you are satisfied with only specific method invocations then parsing the source code for the patterns listed above will give you a minimal list of methods that may be called during execution. But the list may be both incomplete (i.e., not contain methods that may be called) as well as overcomplete (i.e., may contain methods that are not called in practice).
Another great tool is class-dump which was always my first choices for static analysis.
otool -oV /path to executable/ | grep name | awk '{print $3}'

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!

Intercepting Method Access on the Host Program of IronPython

Greetings,
Most of the information I see around concerning the construction of Proxies for objects assume that there exists a Type somewhere which defines the members to be proxied. My problem is: I can't have any such type.
To make the problem simpler, what I have is a dictionary that maps strings to objects. I also have getters and setters to deal with this dictionary.
My goal then is to provide transparent access inside IronPython to this getters and setters as if they were real properties of a class. For example, the following code in a python script:
x.result = x.input * x.percentage;
...would actually represent something like in the host language:
x.SetProperty("result", x.GetProperty("input") * x.GetProperty("percentage"));
Also, 'x' here is given by the host program. Any ideas? Please remember that I cannot afford the creation of a typed stub... Ideally, I would be happy if somehow I could intercept every call to an attribute/method of a specific object in the script language onto the host program.
This post might be useful.