I’m new to iOS development (and Obj-C), and I’m trying to port an existing C program to iOS.
The C program usually runs in the console, but I want to make a UI for it on the iPhone. I’ve already ported the C code, and when the simulator is run I can get the printf output in the console window. I want to avoid changing the original code as much as possible, so this is my plan:
- The program takes some time to execute, so I think I need to run it on a seperate thread. It look likes I’ll only need an NSInvocationOperation to call it’s main method.
- I will redirect stdout to a pipe.
- On another thread, I will read from the pipe, and throw this to the UI. I’m not sure what might be the best concurrancy API to use for this.
Is this a good strategy for the iOS, or is there a better alternative for porting this? Are there any pitfalls I should look out for?
For concurrency, use the dispatch queues for quickest programming. See this guide: http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html
To print to the screen, you could do this in many different ways, but just use a
UILabelif you just want get text up there right away. You can also format it nicely later.Main pitfalls for multithreading are like on any OS – locking any data models that have simultaneous read/write. You can use
@synchronizeor make your dispatch queues thread safe by usingdispatch barriersalso noted in the linked guide above.