Search code examples
objective-ccocoamacosnsoutputstream

NSOutputStream giving -1 bytes?


NSDictionary *bundle = [NSDictionary dictionaryWithObjectsAndKeys:message,@"message", toUserName, @"receiver",fromUserName, @"sender", nil];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:bundle];           
NSOutputStream *outStream;
[toUser getInputStream:nil outputStream:&outStream];
[outStream open];
NSInteger bytes = [outStream write:[data bytes] maxLength: [data length]];
[outStream close];

success = YES;
NSLog(@"Wrote %ld bytes", bytes);

I'm getting: Wrote -1 bytes.


Solution

  • From the write:maxLength: method documentation:

    Return Value
    The number of bytes actually written, or -1 if an error occurs. More information about the error can be obtained with streamError. If the receiver is a fixed-length stream and has reached its capacity, 0 is returned.

    The -1 return value means that an error occurred. You should use [outStream streamError] to get an NSError object telling you what went wrong so you can try to fix it, or to get a description of the problem for the user.