Standard input/output in Objective-C

2010-03-23 Thread Mikael Wämundson
First: Yes, I've been looking around on the net to find an answer ;)

I really like the C++ method of using cin/cout in the iostream.h when 
communicating via the standard input/output (i.e. the Terminal). 

When doing this in Objective-C it gets, to me, very complicated. Seems I'm 
going wrong some way.

My first attempt is of course to just use the cin/cout methods by importing 
iostream.h, but when looking for the header file I find it deeply buried 
(/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/IOKit/stream)
 and I don't succeed in importing it.
My conclusion from this is: I'm not supposed to use cin/cout in an Objective-C 
application???

Second attempt is to use Objective-C methods like:

NSFileHandle *fileHandle = [NSFileHandle fileHandleWithStandardInput];
NSData *inputData;
NSString *inputString;

Person *aPerson = [[Person alloc] init];

printf(Type the persons first name: );
inputData = [fileHandle availableData];
inputString = [[NSString alloc] initWithData: inputData 
encoding:NSUTF8StringEncoding];
[aPerson setPersonsFirstName: inputString];

 Feels complicated. My conclusion: I'm making it more complicated than it 
should be???

Happy for feedback!

/Mikael___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Standard input/output in Objective-C

2010-03-23 Thread Brian Postow

On Mar 23, 2010, at 5:44 PM, Mikael Wämundson wrote:

 First: Yes, I've been looking around on the net to find an answer ;)
 
 I really like the C++ method of using cin/cout in the iostream.h when 
 communicating via the standard input/output (i.e. the Terminal). 
 
 When doing this in Objective-C it gets, to me, very complicated. Seems I'm 
 going wrong some way.
 
 My first attempt is of course to just use the cin/cout methods by importing 
 iostream.h, but when looking for the header file I find it deeply buried 
 (/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/IOKit/stream)
  and I don't succeed in importing it.
 My conclusion from this is: I'm not supposed to use cin/cout in an 
 Objective-C application???


Did you try using an objective-C++ program? in Xcode, if you make all of your 
.m files into .mm files, it should automatically compile as objective-c++ and 
then cin and cout may work...

Also, you shouldn't need to do more than

#include streamio.h 
like normal... the compiler should know where to find it.


Brian Postow
Senior Software Engineer
Acordex Imaging Systems

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Standard input/output in Objective-C

2010-03-23 Thread Nick Zitzmann

On Mar 23, 2010, at 3:44 PM, Mikael Wämundson wrote:

 My first attempt is of course to just use the cin/cout methods by importing 
 iostream.h, but when looking for the header file I find it deeply buried 
 (/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/IOKit/stream)
  and I don't succeed in importing it.
 My conclusion from this is: I'm not supposed to use cin/cout in an 
 Objective-C application???

Wrong. You can use C++ in ObjC++ source. The header you are looking for is 
iostream and it is part of the standard set of headers installed somewhere in 
/usr/include. Ignore the headers in the kernel framework unless you're making a 
kernel extension, because the kernel framework is only to be used for making 
kernel extensions. Kernel extensions use a stripped-down version of C++.

To turn an ObjC source file into an ObjC++ source file, change its extension 
from .m to .mm.

 Feels complicated. My conclusion: I'm making it more complicated than it 
 should be???

If you feel more comfortable using stdout/stdin or cout/cin, then go ahead and 
use them instead.

Nick Zitzmann
http://www.chronosnet.com/

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Standard input/output in Objective-C

2010-03-23 Thread Mikael Wämundson
Thnx!

Just changing the suffix to .mm and rebuilding worked and the library was 
found! Great!

/Mikael

23 mar 2010 kl. 22.56 skrev Brian Postow:

 
 On Mar 23, 2010, at 5:44 PM, Mikael Wämundson wrote:
 
 First: Yes, I've been looking around on the net to find an answer ;)
 
 I really like the C++ method of using cin/cout in the iostream.h when 
 communicating via the standard input/output (i.e. the Terminal). 
 
 When doing this in Objective-C it gets, to me, very complicated. Seems I'm 
 going wrong some way.
 
 My first attempt is of course to just use the cin/cout methods by importing 
 iostream.h, but when looking for the header file I find it deeply buried 
 (/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/IOKit/stream)
  and I don't succeed in importing it.
 My conclusion from this is: I'm not supposed to use cin/cout in an 
 Objective-C application???
 
 
 Did you try using an objective-C++ program? in Xcode, if you make all of your 
 .m files into .mm files, it should automatically compile as objective-c++ and 
 then cin and cout may work...
 
 Also, you shouldn't need to do more than
 
 #include streamio.h 
 like normal... the compiler should know where to find it.
 
 
 Brian Postow
 Senior Software Engineer
 Acordex Imaging Systems
 
 

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Standard input/output in Objective-C

2010-03-23 Thread Jens Alfke


On Mar 23, 2010, at 2:44 PM, Mikael Wämundson wrote:


inputData = [fileHandle availableData];
inputString = [[NSString alloc] initWithData: inputData  
encoding:NSUTF8StringEncoding];

[aPerson setPersonsFirstName: inputString];


You could just call fgets and then convert the resulting C string to  
an NSString.


I think the reason there isn't anything Cocoa-like for this is that  
hardly anyone uses Objective-C to write interactive console tools.


—Jens___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com