0

I've consulted a similar thread to the following code that I have so far. Unfortunately, this doesn't work as planned. The following code produces no error/warning but will do nothing. I want the IBOutlet class level because it will be used in a class level method as stated below.

#import <Foundation/Foundation.h>

@interface Interface : NSObject <NSApplicationDelegate> {NSTextView* textView;}

@property (nonatomic, retain) IBOutlet NSTextView* textView;

+ (void)NSPrint:(NSString*)aString;

@end

meanwhile in Interface.m..

 #import "Interface.h"

@implementation Interface
@synthesize textView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [Interface NSPrint:@"Have fun with this program!"];
}
+ (void)NSPrint:(NSString*)aString
{
    [[[[[Interface new] textView] textStorage] mutableString] appendFormat:@"%@\n",aString];
}
evdude100
  • 437
  • 4
  • 18

1 Answers1

2

I'm not really sure what you're doing here. NSPrint creates a instance of Interface, and changes a property of one of its instance variables. Then the method ends.

What are you expecting to observe from this? You don't have the instance of Interface created in NSPrint: connected to anything or doing anything- you just create it and leave it floating free. To actually access the instance of Interface created by [Interface new], use something like:

+(Interface*)NSPrint:(NSString*)aString
{
      Interface* newInterface = [Interface new];
      [[[[newInterface textView] textStorage] mutableString] appendFormat:@"%@\n",aString];
      return newInterface;
}

On the subject of IBOutlet: IBOutlet has no meaning except to alert XCode's interface builder that it should allows you to create connections between a controller object's variable (marked with IBOutlet) and another object created in the interface builder. Then, when the view is loaded, the controller object's variable's setter method is called using the connected object as the argument. You end up with an instance of the controller class with its instance variable set to an object created in the interface Builder.

For more info on that, look in the Objective-C manual on the Apple website: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html or at the information about interface builder: http://developer.apple.com/library/mac/#recipes/xcode_help-interface_builder/_index.html#//apple_ref/doc/uid/TP40009971

If you need a class variable (that is, a variable connected to a class and not to a specific instance of that class), you have a problem. Take a look at this question for more on that: Objective C Static Class Level variables

Community
  • 1
  • 1
Daniel
  • 1,079
  • 1
  • 11
  • 25
  • Interface is the class name and not an instance of itself (?). The code I posted was short hand from this solution: http://bit.ly/pez8cH I have no idea what solution you posted, and it throws a compiler error. I am saying that I want to call NSPrint from a different class. I do not want to return newInterface. I want to append the string and NOT return an instance of the class, which I have no idea what means it. – evdude100 Aug 22 '11 at 02:48
  • The problem is that NSPrint will not do anything as it is now. `[Interface new] creates a new object by calling Interface's alloc and init methods. You then set the string of that object to aString. If you want a constant class variable, take a look at the question I added a reference to in my answer – Daniel Aug 22 '11 at 03:12
  • I still don't understand how static class level vars are going to work in this solution. I don't think I need aString as class level. So far this is what I have gathered from your answer to my question: "the code I posted doesn't work. Theres a problem that I can't solve or post code that actually relates to the situation so I'll say something obscure about aString and somehow tie it in to the actual problem". I'm not trying to be rude but word for word that is all I got out of everything you have said. – evdude100 Aug 22 '11 at 03:38
  • Again, I don't understand what you want to do. From your code, I get that you want to have a string that can be changed by calling a class method. You want that string to be held by a text view, which is an instance variable of the class Interface. Instance variables can only be accessed from an instance of the class, not from the constructor, which is what the class level method affects. So, could you explain a bit more what you're trying to do? The code you wrote is technically valid code - I just don't understand what you want it to do instead of what it does now. – Daniel Aug 22 '11 at 04:03
  • Here is what I want: Send NSPrint to the class Interface with the argument-->@"TESTING." aString is appended to the textView. I now see aString in my text view. Of the code you posted does not work for me. Regarding static variables, although I don't know why I added a static NSString* aString and that had no affect. – evdude100 Aug 22 '11 at 14:19
  • @evdude100 let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2739/discussion-between-daniel-and-evdude100) – Daniel Aug 22 '11 at 16:10
  • I have one more question in the chat – evdude100 Aug 25 '11 at 03:11