yellow
yellow

Reputation: 463

Redirecting printf on iOS to user interface

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:

  1. 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.
  2. I will redirect stdout to a pipe.
  3. 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?

Upvotes: 2

Views: 699

Answers (1)

johnbakers
johnbakers

Reputation: 24770

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 UILabel if 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 @synchronize or make your dispatch queues thread safe by using dispatch barriers also noted in the linked guide above.

Upvotes: 1

Related Questions