This question is basically the inverse of this other question: Calling Python from Objective-C
I have implemented my iPhone application logic in Objective-C (obviously), and am now trying to re-use as much as possible from my XCode project in the server component to save on double-implementation. I have successfully loaded the CoreData data model from Python, however, can't see a way to actually call into the Objective-C logic from Python.
Basically I'm trying to access the Objective-C classes and methods in my iPhone project from Python to save myself duping out all the implementations.
Is this even vaguely possible, or is dupe-implementation the only solution here? Seems like the kind of thing Boost::Python might be used for, but I'm not really sure.
edit: Boost::Python won't work because it is C++ based and I need Objective-C. I knew there was a reason why that didn't work.
If your Objective-C code is in a framework and you would like to essentially write a Python application that uses your framework, then you can use objc.loadBundle, and then use objc.lookUpClass or NSClassFromString to get access to your classes. From there, you can use your classes like any other bridged Objective-C class.
If you're running your Python code within a process that already has the Objective-C runtime up, and your classes are already registered with it, then you can skip the loadBundle step.
Related
In a blog post I have just read:
'Swift allows us to extend classes from NSObject to get Objective-C runtime features for an object. It also allows us to annotate Swift methods with #objc to allow the methods to be used by the Objective-C runtime.'
I don't understand the term objective-C runtime features. Is it meaning that the code could be used in a objective-C project as well?
Quoting the apple docs
The Objective-C runtime is a runtime library that provides support for the dynamic properties of the Objective-C language, and as such is linked to by all Objective-C apps. Objective-C runtime library support functions are implemented in the shared library found at /usr/lib/libobjc.A.dylib.
That API is useful primarily for developing bridge layers between Objective-C and other languages, or for low-level debugging. You most likely don't need to use it.
Even when written without a single line of Objective-C code, every Swift app executes inside the Objective-C runtime, so that's why you can access it.
You can do things like swizzling
Objective-C Runtime
A lot of programming languages is ship with a kind of runtime/standard library which includes some core/base functionality
Objective-C Runtime is responsible for dynamism, memory management(allocation, reference counting...) and other language features. It is a layer between compiler and core libraries/frameworks.
Objective-C is dynamic language because shift focus from compile time to runtime:
Dynamic typing - figure out object's class in runtime. inheritance and other OOP principles
Dynamic binding - figure out invoked method in runtime. Message dispatch mechanism[About]
Dynamic loading - you are able to lazy-loading a new module in runtime.
Other features:
basic structures like class, object, variable, property, method
functions for working with these structures
Introspection - get/read info about class/object in runtime. For example instance variables, methods names, method arguments. class_getName
Reflection - modify its own structure and behavior. For example allocate new class, add variable, add method. class_addMethod
objc_msgSend - based on message dispatch
Swizzling - swap method realisation in runtime. method_exchangeImplementations. [Objective C], [Swift] swizzling example
Using [#objc vs dynamic] expose Swift's API for Objective-C and adds a dynamic behaviour for Swift code. It is useful when you need something which is not possible to do with usual swift approaches. Good examples are KVO, swizzling...
[KVO]
Objective-C vs Swift
Swift can use Objective-C Runtime library.
Swift has better performance(based on table dispatch) but Objective-C is more dynamic language
In my hunt for various GUI frameworks, I remembered GTK being a fairly mature library for making user interfaces for desktop applications.
And it got me wondering, is it possible to use the Glade interface designer with Scala? Is there perhaps a Scala-optimised framework or library to make the process more painless?
It appears that there exists a GTK+ binding for Java, and from an unofficial source it seems like you can use this in Scala.
This binding contains class Builder with method addFromFile(), so you should be able to create a UI file in Glade, save it, then load it in your Scala project, and use the method getObject() to access the objects defined in Glade.
For more details you can refer to this question and its answers, which deal with Java, but should be very similar to Scala.
As Scala is interoperable with Java, you can use it's bindings. I highly recommend you to first see a scala REPL micro tutorial for Ubuntu then read and understand what is GtkBuilder and how to create Gtk+ interfaces using Glade. Then a proposed process could be:
Create your apps UIs using Glade
Call GtkBuilder in your Scala code to interact with UI parts (send receive data)
Create Scala script file for each UI
Create a separate backend app with REST interface to present app input/ouput as UI state changes
Connect the two apps
By doing so, you'll get a set of bash scripts (written in Scala) each draw a native UI when executed. Whenever the backend is running, it's possible for UI to be used as I/O of backend app.
Has anyone developed a singleton for accessing SQLite db? I know that there are other options like Core Data but, in my case, I need SQLite. I have looked at Apple provided singleton creation code (here) but the thing is SQLite database "stuff" is not an object, it is "typedef struct sqlite3". So currently, I'm doubting how should I adapt this code for being singleton. Any suggestions, please :)
UPDATE: I have looked at FMDB framework for SQLite that does all stuff but it doesn't implement singleton. I mean, access methods are instance not class methods. If I need to call the SQL statements from different my code places I need to pass a pointer around instead of calling some shared instance class method :( So, the question remains open.
I think you should use FMDB, it is one of good wrapper libraries around for SQLite3.
See details about FMDB here http://www.ioslib.com/library/data/fmdb/
I recently read about Dynamic Creation as one of the design pattern in Cocoa. However, I don't really understand how it works. So I need clarification from you who have implemented in your design.
What is it? Why and when would you use this design pattern?
I have read that you use NSClassFromString() to access the class. I assume that I use this when I want to use class that doesn't exist within the project I'm working on. Usually when I want to use certain class, I imported them in header. Does using this approach skip the #import process?
Class JavaArrayList = NSClassFromString(#"java.util.ArrayList");
I quote the code above as example. If do according to the code above, that means I can create a new JavaArrayList class and use the methods in it right?
JavaArrayList *foo = [[JavaArrayList alloc] init];
[foo useMethodBelongJava:doWhateverTask];
What are the benefits of using this design pattern? Especially in iPhone Development.
Your example appears to be using that pattern to instantiate a Java class. In the old days (up to about MacOS 10.4 I think), Apple had some technology called the Cocoa-Java Bridge, which let you use Java classes within Objective-C code. You had to instantiate them in the manner specified, because they didn't have Objective-C header files to import.
However, as of Snow Leopard, the Java Bridge no longer exists, so the code in your question won't work any more.
The recommended solution for calling a Java class from Objective-C is now JNI. Take a look at this question if that is what you're trying to do.
What is it? Why and when would you use this design pattern?
Coming back to NSClassFromString, it has other uses besides instantiating Java classes (which, as I mentioned, it doesn't do any more!). For an example, recently I wrote a library for parsing the response from a web service. In order to make it work with different web services, I had it read in a configuration file that described the data format it was expecting. For each field in the web service, my configuration file specified which Cocoa class to instantiate. Thus, in my code, I had a Cocoa class name as a string. To instantiate the object I wanted, I used NSClassFromString to turn it into a Class object.
Usually when I want to use certain class, I imported them in header. Does using this approach skip the #import process?
It can do. NSClassFromString will instantiate any class that is present at run time, so you don't need the header to be able to use it. If you don't have the header, you'll get a bunch of warnings of "may not respond to selector" whenever you try and use your newly instantiated class, as the compiler doesn't have enough information to be helpful. However, in many circumstances where NSClassFromString is useful, the header files aren't available.
See this link:
need advise about NSClassFromString
The only real benefit for iPhone was being able to reference classes from newer APIs and still target the old APIs. Since 4.0 you can do this anyway by setting the deployment target of your project. I can't really see any reason you would use it for iPhone programming any more.
This would only work for objective-C classes. You can't import java objects into your iphone app.
When a user uploads something via my app, I create an ASIFormDataRequest object to make the POST.
When the user is offline, I would like to write a ASIFormDataRequest object to file and send it later.
Is there a built in way to serialize an object like this in Objective C, or do I have to write something from scratch?
Yep! There's a really great thing called the NSCoding protocol. A writeup on how to implement and use it is available on our local CocoaHeads site: http://cocoaheads.byu.edu/wiki/nscoding In a nutshell, you implement two methods to define what you want to save and how to restore it, and then it's a one-liner to actually archive your object.
In the Objective-C programming
language, serialization (more commonly
known as archiving) is achieved by
overriding the write: and read:
methods in the Object root class.
http://en.wikipedia.org/wiki/Serialization#Objective-C
There's a code example there too :-)