Guest User

Untitled

a guest
May 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. // name: first.mm
  2. // description: STL -> ObjectiveC conversion
  3. // compile: c++ first.mm -ObjC++ -framework Foundation
  4.  
  5. #import <Foundation/Foundation.h>
  6. #include <string>
  7. #include <vector>
  8. using namespace std;
  9.  
  10. NSArray *ToArray(const vector<string>& source) {
  11. NSMutableArray *result = [NSMutableArray arrayWithCapacity:source.size()];
  12.  
  13. vector<string>::const_iterator it = source.begin();
  14. for ( ; it != source.end(); ++it) {
  15. const string& ss = *it;
  16. NSString *rs = [[[NSString alloc] initWithBytes:ss.c_str() length:ss.size() encoding:NSUTF8StringEncoding] autorelease];
  17. [result addObject:rs];
  18. }
  19.  
  20. return result;
  21. }
  22.  
  23. int main(int argc, char **argv) {
  24. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  25.  
  26. vector<string> v;
  27. for (int i = 1; i < argc; i++) { v.push_back(argv[i]); }
  28.  
  29. NSArray *w = ToArray(v);
  30. for (NSInteger i = 0; i < [w count]; i++) {
  31. NSLog(@"%d: %@", i, [w objectAtIndex:i]);
  32. }
  33.  
  34. [pool release];
  35. return 0;
  36. }
Add Comment
Please, Sign In to add comment