• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Converting NSString to C++ std::string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Converting NSString to C++ std::string


  • Subject: Re: Converting NSString to C++ std::string
  • From: Andrew Wood <email@hidden>
  • Date: Sat, 9 May 2009 13:45:03 +0100

The controllers header file is as follows. As you can see I decalre dbhost as a std::string, but dont initialise it.

The initalisation (which crashes) is done in the doLogin action method.

#import <Cocoa/Cocoa.h>
#include <string>


@interface CoreController : NSObject { //App Windows IBOutlet NSWindow* loginwindow; IBOutlet NSWindow* editwindow; IBOutlet NSWindow* mainwindow; IBOutlet NSWindow* aboutwindow; //Login window outlets IBOutlet NSTextField* dbhostUIfield; IBOutlet NSTextField* usernameUIfield; IBOutlet NSTextField* passwordUIfield; IBOutlet NSTextField* dbnameUIfield; IBOutlet NSTextField* dbportUIfield; IBOutlet NSProgressIndicator* connectingspinner; //Edit window outlets IBOutlet NSTextField* internalhost; IBOutlet NSTextField* internalport; IBOutlet NSComboBox* subsystype; IBOutlet NSTextField* externalhost; IBOutlet NSButton* allinterfaces; IBOutlet NSTextField* externalport; IBOutlet NSButton* tcp; IBOutlet NSButton* udp; //Business logic instance vars std::string dbhost; std::string dbname; std::string username; std::string password; int dbport; } -(IBAction)save:(id)sender; -(IBAction)doLogin:(id)sender; -(IBAction)showAboutPanel:(id)sender;

-(void)applicationDidFinishLaunching:(NSNotification*)aNotification;
@end

On 9 May 2009, at 13:34, Andrew Wood wrote:

Investigated it a bit further. It works if I declare the string in the method where Im doing the assignment, but what I was trying to do, and really need to do, is decalre the std::string in the app controllers header file, so that its visible to all methods in the controller, then do the assignment in an action method which is called when a button is clicked.


On 8 May 2009, at 17:56, Drew Lawson wrote:

According to Alex Curylo:


On 8-May-09, at 8:55 AM, email@hidden wrote:

mycppstring = std::string([[myNSTextField stringValue] UTF8String]);


But it keeps crashing. What's the recommended way?

I have no idea, but it works for me to simply assign it without the constructor. Why, here's an example in the file I'm working on right now, fancy that:

The only other gotcha I can think of (because it annoys me) is that std::string does not play nice with null pointers. Is it possible that stringValue returns nil?

Hmm, I had one other thought.  There are several std::string
constructors, depending on the parameter type.  In ObjC++, is it
unambiguous at compile time that the expression is "const char*"?

--
Drew Lawson            |  We were taking a vote when
                     |  the ground came up and hit us.
                     |      -- Cylon warrior
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Converting NSString to C++ std::string
      • From: "Stephen J. Butler" <email@hidden>
References: 
 >Re: Converting NSString to C++ std::string (From: Alex Curylo <email@hidden>)
 >Re: Converting NSString to C++ std::string (From: Drew Lawson <email@hidden>)
 >Re: Converting NSString to C++ std::string (From: Andrew Wood <email@hidden>)

  • Prev by Date: Custom Sheets
  • Next by Date: Listing running process
  • Previous by thread: Re: Converting NSString to C++ std::string
  • Next by thread: Re: Converting NSString to C++ std::string
  • Index(es):
    • Date
    • Thread