Is it possible for me to read iOS source code for classes such as CFWriteStreamRef and NSOutputStream? - cocoa-touch

I would like to review for myself the error handling and buffering that Apple has implemented on top of their implementation of sockets.
Is it possible for me to read that code?
Thank you

Related

Is it OK to implement a protocol that's unavailable in my deployed OS version?

Say I'm writing an app to run on iOS7 and iOS6. I've a class that implements a protocol - a protocol only available in iOS7 (in this case, NSLayoutManagerDelegate).
Everything seems to work on iOS6 - my code that instantiates the class implementing the protocol is skipped on iOS6 - so it appears I'm good to go.
I was a little surprised not to see an error during app load though; apparently the loader is OK with me implementing a protocol that doesn't exist. Luckily for me!
Is there any Apple documentation that discusses this scenario? Is it safe? Any gotchas I should watch for?
No problem with that, a bit like implementing a method that is only called by the system for some version upwards.
At worst you can say that you have generated some code that takes up space somewhere (the "disk" if you can call it that on iOS and probably also the RAM) without being used on the older version(s), but that is minimal.
This all requires that the code is compiled with an SDK that includes the protocol in question - if conformance to the protocol is declared by using the <ProtocolName> construct and including the corresponding header. But that is kind of a given, since it won't compile otherwise.

Reporting a program exception/error in an objective C program back to developer

I am porting a C# application to mac osx,
In the C# version of the program we have a try catch() block which catches any exceptions thrown by the program , which may be nullpointer exceptions or anything we've not caught..
The top level exception handler then asks the user if he likes to report it, when the user says report,
the exception stack is reported to us. which helps identify bugs.
In my OSX app the backend part of the program in C/C++ with User interface in Objective C
Is there a similar feature in an objective C program ?? if not is there any alternate way to report, exception or bug report from within the program ?
As far as exceptions go, NSError is actually preferred over exceptions in Objective-C for non-fatal errors. This is described in Apple's Exception Programming Topics: Introduction to Exception Programming Topics for Cocoa documentation:
Important: You should reserve the use of exceptions for programming or unexpected runtime errors such as out-of-bounds collection access, attempts to mutate immutable objects, sending an invalid message, and losing the connection to the window server.
...
Instead of exceptions, error objects (NSError) and the Cocoa error-delivery mechanism are the recommended way to communicate expected errors in Cocoa applications. For further information, see Error Handling Programming Guide.
Using NSError to Great Effect is a good tutorial on NSError (in addition to the afore-linked error handling documentation).
As far as crash/fatal error reporting goes, see the "Crash Reporter for Cocoa app" question for a number of the options.

How to send message asynchronously in Objective C

Sending message in Objective C is not asynchronous according to answer I got Is sending a message in Objective C actually asynchronous?
so my next question is how to actually send message asynchronously in Objective C (within same application) is it even possible (without resorting to complex stuff like threads) ?
Check Apple's "Concurrency Programming Guide" in the doc set. You have options such as threads, operation queues, dispatch queues, and more. It's a big subject.
Assuming you are trying to get something to happen off the main thread there are numerous ways to handle it. Which one you use depends on what you are doing.
Check out these Apple docs to start with:
https://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html#//apple_ref/doc/uid/TP40008079
https://developer.apple.com/library/mac/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html#//apple_ref/doc/uid/10000057i
if you want to do real async method calling using Apple frameworks, just have a look to GCD, and more precisely to dispatch_async.
GCD is available for both iOS and OSX and Apple knows what's coming next, so using this "asycn API" will ensure you to be the less prone to update incompatibility.
good reading ;)
edit: ok, if you really don't want any thread, you can declare the method you are calling as (oneway void). I found that while overriding release.
Here's a SO answer explaining what it does: https://stackoverflow.com/a/5495195/700317
hope this helps.

Implementing Progress Indictor For NSURLConnectionDelegate protocol

I'm trying to do some progress indication for when trying to upload and download a file.
Been reading and looks like this is the method that I need to use.
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
I'd like to read more about how that function works (i.e when is it called etc), but can't seem to find the reference documentation. I went to this URL but it does not say anything about that particular function.
https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html%23//apple_ref/occ/intf/NSURLConnectionDelegate
Where can I read more about it?
Thank you,
Tee
NSURLConnection had an informal protocol for its delegate. That changed in iOS 5 (I think the same happened on OSX). They deprecated the methods declared in NSURLConnection and moved them into formal protocols NSURLConnectionDelegate and NSURLConnectionDataDelegate.
And now the fun part. They deprecated the methods, they even removed them from the NSURLConnection documentation but they did not document the new formal protocols.
Currently, only NSURLConnectionDelegate is documented. NSURLConnectionDataDelegate is not mentioned anywhere.
There are two ways how to find what the method does.
Look into the previous version of NSURLConnection docs (e.g. iOS 4.3). I would give you a link but I couldn't find it online. Maybe you have the library downloaded in your XCode
Press cmd-shift-o in Xcode, type NSURLConnectionDataDelegate and press enter. You have found the header and the methods have a description there:
connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:
is called during an upload operation to provide
progress feedback. Note that the values may
change in unexpected ways if the request needs to
be retransmitted.
Try AFNetworking It is beocming a popular replacement of ASIHTTPRequest, if you are concerned about support. Also it has a built in support for download progress. (See discussion on this link.) Here is a recent discussion of how to actually use it.
UPDATE
How do I track upload or download progress? (AFNetworking faq)
AFURLConnectionOperation provides the methods setUploadProgressBlock: and setDownloadProgressBlock:. Each method takes a single parameter, which is a block that will be executed periodically during the lifetime of the request. The block has no return type and takes 3 arguments: the number of bytes read or written for this callback, the total number of bytes read or written so far, and the total number of bytes expected to be read or written.
If you wanted to update a progress bar, you could set the respective progress block to set the progress amount to total number bytes read or written divided by the expected number, normalized between 0.0 and 1.0. UI updates based on this value will update asynchronously as the request is being made.

clever ways of tracking down bugs in Obj-C

I'm having a bug in my Objective C program which causes the machine to crash hip deep in some library methods, and it's all library methods down the stack to main (Which I haven't touched from the one XCode gave me). So, I have a bit of a mystery.
The error I'm getting is:
Program received signal: “EXC_BAD_ACCESS”.
Now, I'm sure that this means that somewhere I'm releasing something too many times, or something like that. This is the objective C version of a seg-fault, right?
My question is: Since it's not happening in my own code, is there some clever way of tracking down what I'm double releasing? or is code inspection the best bet?
thanks.
EXC_BAD_ACCESS essentially means that you're trying to access or use a specific chunk of memory in an unexpected way. For example, if you try to send a message to a memory reference that no longer represents a valid object. It's different from a segmentation fault, but related.
See this related SO question for suggestions on debugging over-released objects. NSZombie will work wonders for you. Once you get your hands on Snow Leopard (you're getting it this Friday, right?) use the Zombies instrument to simplify the process, and use the Xcode static analyzer to help you find such errors at compile time.
Also visit: http://www.cocoadev.com/index.pl?DebuggingTechniques and this Apple Tech Note.