How to convert Objective C textual (po) representation to real object - iphone

In xcode you can use po object to see a textual representation of a given object. Is it possible to convert from this textual representation to a real objective c object?
Thanks

I suppose you could parse out the address, assign it to a pointer, and retrieve the object from memory that way, but that IS A HORRIBLY BAD IDEA AND YOU SHOULD NEVER DO THAT.
Real question: what are you trying to do?

I have a project that may inspire you, its on GitHub and its called NDJSON. Basically it uses runtime introspection to get properties an object has and set those properties using a json key of the same value. You could do something like that but get the property values instead of set. To help in situation where the type can not be determined like the type of objects to go in a collection or to map a json key to differently named property I have defined an informal protocol that has some methods that return dictionary mapping json key to property names or class to use for property name, there are also method to return a list of json keys to ignore or alternatively the only keys to accept. If you look at my code on git hub there is a class called NDJSONDeserializer which contains this logic. There is a function called getTypeNameFromPropertyAttributes() which parses out the string result of Apples runtime method property_getAttributes() which you will want to look at.

No, the representation you see from po <instance> is the result of -[<instance> debugDescription], which by default returns -[<instance> description], as described in http://developer.apple.com/mac/library/technotes/tn2004/tn2124.html.
Unless the instance you're dealing with just happens to provide a description which is a serialized form of itself, you're SOL. Most objects don't.
The real question, as Dave points out is, what are you trying to do? po only works in the gdb console, so I assume this is a debugging issue. In that case, do you know that the gdb console supports sending messages to instances? So you can do:
po [<myInstance> methodReturningAnOtherObject]
or
po [<myInstance> valueForKeyPath:#"some.long.key.path"]
to follow the object graph from within the debugger console.

Related

Passing An Object Instance By Value In Dart

I have an object that was created with GetxController and it has a list field. I want to select a specific item in that list and change some parameters. But I should be able to cancel this changing scenario. Because when I select an item and change it, also change that class list item.
In Dart, objects pass by reference. But I need to pass it by value for canceling change. There's any way to pass an object instance by value?
In this kind of scenario, you should try to use immutable types.
There are good Dart libraries for that, even if the language doesn't have native support for it... with truly immutable data types, you essentially get pass-by-value semantics. They also make it easy to make copies of your data with small modifications on the returned value (while the original obviously remains unmodified).
Example packages you could use:
built_collection
built_value
freezed
No.
But you can just make a copy yourself and pass that. And depending on whether the change was confirmed you can eiter copy it back into your list or not.

Is it possible to get the type of a variable while computing completion suggestions?

I'm thinking of creating a VSCode extension to help people use a library I am creating. I've looked in 50 places and can only see documentation on getting the plain text of the document in which the completion suggestions are triggered.
My library exposes a function with two parameters, both objects with the same keys, but the first one will be defined already and the 2nd one will be passed in as an object literal. For example
const obj1 = {a:1, b:2};
doLibraryThing(obj1, {a:[], b:[]});
I want to provide code completion, or a snippet, or anything that can detect that doLibraryThing is being called, and know what properties were defined in obj1, even though usually it will be imported from another file; and then use those properties to generate the suggestion with the same properties, {a:[], b:[]}.
Is this possible?

In libseccomp, what is scmp_filter_ctx? And what is it used for?

What actually stores scmp_filter_ctx since it's defined as void?
See seccomp header file.
In general what is a context ?
It's not defined as void but void *, so it's a generic pointer.
From a quick glance at the file you link I would say it points to some object which structure you can't access (and don't need to know about). The context seems to be the object storing the information seccomp needs to work, and you only need to init, update or use it through the API functions exposed in that header file.
If you really want to see what's inside, just read the source :). In src/api.c it's apparently casted as a struct db_filter_col *, which of course is not exposed to users.

make play-json read the empty string as None for a type of Option[T]

I'm attempting to parse json from the GitHub API with play-json, and encountering a problem with the merge_commit_sha field on Pull Requests (incidentally, I know this field is deprecated, but don't want to discuss that in this parsing problem!). Unfortunately merge_commit_sha field comes back as the empty string in some cases:
"merge_commit_sha": ""
This is how the field is declared in my case class:
merge_commit_sha: Option[ObjectId],
I have an implicit Format[ObjectId], which does not tolerate the empty string, because that's not a valid value for a Git hash id. I'm also using a play-json macro-generated Read[PullRequest], which I'd like to keep on using, in preference to individually declaring reads for every single field on pull requests.
As I've declared the field to be an Option, I'd like "merge_commit_sha": "" to be read as the value None, but this is not what currently happens - a string is present, so the Format[ObjectId] is invoked, and returns a JsFailure.
One thing I tried was declaring an implicit Format[Option[ObjectId]] with the required behaviour, but it didn't seem to get used by the macro-generated Read[PullRequest].
You can define a custom Reads and Writes yourself.
Using Json.format[MyType] uses a Scala macro. You may be able to hook into that. Although, 'extending' a macro for this one case class just seems wrong.
Custom Reads and Writes might be a little 'boilerplate-like' and boring, but they have their upsides.
For example if your json has a bunch of new fields on it, you wont get a JsError when validating or transforming it to a case class. You only take what you need from the JSON and create objects. It also allows for a separation between your internal model and what you're consuming, which in some cases is preferred.
I hope this helps,
Rhys
EDIT
After using some other JSON libs I may have found what you are looking for.
I know the question was asking specifically after Play JSON.
If you're able to move away from Play JSON, Look at spray-json-shapeless specifically JsNullBehaviour and JsNullNotNone REF.

Parse xml file with same tag multiple times iphone sdk

In my application, I have a tag multiple times. I'm using xml parser. I'm taking a corresponding element with similar name as the one in xml file in my class. So in case of:
<photo>abc</photo>
<photo>def</photo>
What I get in photo element of my class is the second element i.e def, as the first one gets overwritten as there's only one photo element in my class. My question is am I wrong in taking similar elements in class as in case of xml? Is there any better method or a better parser? Or I'm on right path and have to do this manually by setting some flags etc?
Thanx in advance.
I assume you try to parse the contents of an XML to match to properties on an object using NSXMLParser.
If it is valid to encounter multiple photo tags in your XML then either you need a strategy to know which one will map to a property, or convert your property to an NSArray and add the results as they come.
If it is not a valid case, you could check if you set the property earlier and raise an error, or just override it (as you do) and call it "undefined behavior".
There are many parsers there that can handle your issue (e.g. XPathQuery, TouchXML etc.).
I don't think that there is a need to reinvent the wheel - use one of the existing parsers.