error: statically allocated instance of Objective-C class 'NSString'

Discussion in 'Mac Programming' started by NSNick, Jul 1, 2008.

  1. macrumors regular

    Joined:
    Jun 27, 2008
    Location:
    Washington D.C.
    #1
    What does "error: statically allocated instance of Objective-C class 'NSString'" mean?

    Also, what does "warning local declaration of 'newData' hides instance variable" mean?
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    The first one i'll let someone else handle. Posting your code might help.

    The second one means that there is a variable that is either globally scoped, a class member, or a function variable and you've redeclared that variable in a lower scope. For example, you have a global variable newData, and in a function you declare another variable named newData. C and Objective-C, as far as I know, doesn't have a scope resolution operator like C++'s ::. As such, you will not be able to access the higher scoped variable with this name in the scope that the new variable is declared. You probably accidentally put the type before the variable in a function when you wanted to use it, etc.

    If you truly want a new variable at the lower scope it would be best to pick a different name.

    -Lee
     
  3. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #3
    The first one just means you said NSString foo instead of NSString *foo
     
  4. macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #4
    second one means...

    U may declared a myVariable as property or global... then again u are declaring the same thing locally...(just rename the second declaration or remove that declrtn.)
     
  5. thread starter macrumors regular

    Joined:
    Jun 27, 2008
    Location:
    Washington D.C.
    #5
    The purpose of my code is to have an NSTextField, button, and an NSTableView. When the button is pressed a new cell is created in the table and the data from the textfield is added into the cell.

    #import <Cocoa/Cocoa.h>
    #import "ItemData.h"

    @interface AppController : NSObject {
    IBOutlet NSTableView *myTable; // Outlet to the table view, connected in IB.
    NSMutableArray *items; // An array of AppointmentData objects.
    }

    - (IBAction)addItem:(id)sender;
    - (IBAction)removeSelectedItem:(id)sender;


    @end


    #import "AppController.h"



    @implementation AppController

    - (void)dealloc {
    [items release];
    items = nil;
    [super dealloc];
    }

    - (void)awakeFromNib {
    // Create the storage our table will use.
    items = [[NSMutableArray alloc] init];

    // Be the data source...
    [myTable setDataSource:self];




    // Let the text in the @"Info" column wrap.
    [[[myTable tableColumnWithIdentifier:mad:"Info"] dataCell] setWraps:YES];

    // Start with atleast one item.
    [self addItem:nil];
    }

    // ---------------------------------------------------------
    // Action methods
    // ---------------------------------------------------------



    - (void)addItem:(id)sender {
    // Append a newly created data object, then reload the table contents.
    ItemData *itemData = [[ItemData alloc] init];
    [items addObject: itemData];
    [itemData release];
    [myTable reloadData];
    }

    - (void)removeSelectedItem:(id)sender {
    // Remove the selected row from the data set, then reload the table contents.
    [items removeObjectAtIndex: [myTable selectedRow]];
    [myTable reloadData];
    }

    // ---------------------------------------------------------
    // Data source methods
    // ---------------------------------------------------------

    - (int)numberOfRowsInTableView:(NSTableView *)tv {
    return [items count];
    }

    - (id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tc row:(int)row {
    return [[items objectAtIndex:row] info];
    }

    - (void)tableView:(NSTableView *)tv setObjectValue:(id)objectValue forTableColumn:(NSTableColumn *)tc row:(int)row {

    [[items objectAtIndex:row] setInfo: objectValue];
    }

    @end


    #import <Foundation/Foundation.h>

    @interface ItemData : NSObject {
    IBOutlet NSTextField *newDataField;
    NSString *info;
    NSString *newData;
    }

    - (void)setInfo:(NSString *)newInfo;
    - (NSString *)info;

    @end


    #import "ItemData.h"



    @implementation ItemData

    - (id)init {
    self = [super init];
    if (self) {
    NSString *newData = @"%@", newDataField;
    [self setInfo: newData];
    }
    return self;
    }

    - (void)dealloc {
    [info release];
    info = nil;
    [super dealloc];
    }

    - (void)setInfo:(NSString *)newInfo {
    if (info != newInfo) {
    [info release];
    info = [newInfo copy];
    }
    }

    - (NSString *)info {
    return [[info retain] autorelease];
    }

    @end
     
  6. thread starter macrumors regular

    Joined:
    Jun 27, 2008
    Location:
    Washington D.C.
    #6
    The error occurs after this line
    NSString *newData = @"%@", newDataField;
    The warning occurs after this line
    [self setInfo: newData];
     
  7. macrumors 68020

    Krevnik

    Joined:
    Sep 8, 2003
    #7
    Two things:

    1) Put your code in code tags. Otherwise it is nearly impossible to read correctly.

    2) Two, this line isn't exactly valid:

    Code:
    NSString *newData = @"%@", newDataField;
    The problem is that you can't create strings like that. Try this:

    Code:
    NSString *newData = [NSString stringWithFormat:@"%@",[newDataField stringValue]];
    And maybe rename newData. As the second poster said, it hides the object variable of the same name. Pretty much means the compiler doesn't know for sure which one you mean.

    Edit: I just realized what you were actually trying to do... tweaked...
     
  8. thread starter macrumors regular

    Joined:
    Jun 27, 2008
    Location:
    Washington D.C.
    #8
    I have made the suggested changes and now there are no errors or warnings; however, now when I click Add, it adds a row that says (null) instead of the data that was typed into the textfield.
     
  9. macrumors 6502

    Joined:
    May 12, 2003
    #9
    Code:
    - (void)setInfo:(NSString *)newInfo {
        if (info != newInfo) {
            [info release];
            info = [newInfo copy];
        }
    }
    
    I don't know if this is the entire problem, but this section seems off. Notice that if info is not equal to newInfo, nothing happens. You'll never be able to set a new value.

    What you want is more like this. It's overcommented, I know, but it's to explain what each statement is doing.

    Code:
    - (void)setInfo:(NSString *)newInfo {
        // If values are the same, do nothing.
        // Use isEqualToString, not ==, as you should care about the strings'
        // values, not whether they are the same object.
        if ([info isEqualToString:newInfo]) {
            return;
        }
        
        // If info is not nil, release it.
        if (info != nil)
            [info release];
    
        // Now assign the value to info, and be sure to retain it
        // or else it will get autoreleased. 
        info = [[newInfo copy] retain];
    }
    
     
  10. thread starter macrumors regular

    Joined:
    Jun 27, 2008
    Location:
    Washington D.C.
    #10
    It doesn't seem to behave any differently with the new code.
     
  11. thread starter macrumors regular

    Joined:
    Jun 27, 2008
    Location:
    Washington D.C.
    #11
    When I change ItemData.m to look like


    #import "ItemData.h"



    @implementation ItemData

    - (id)init {
    self = [super init];
    if (self) {
    //Creates now row with data in text field
    [self setInfo:[NSString stringWithFormat:mad:"%@",[newDataField stringValue]]];
    }
    NSLog(@"newDataField %@", newDataField);
    NSLog(@"newData %@", newData);
    return self;
    }

    - (void)dealloc {
    [info release];
    info = nil;
    [super dealloc];
    }

    //setInfo method for the initialization part of ItemData
    - (void)setInfo:(NSString *)newInfo {
    if (info != newInfo) {
    [info release];
    info = [newInfo copy];
    }
    }

    - (NSString *)info {
    return [[info retain] autorelease];
    }

    @end


    The debugger tells me that both newDataField and NewData are (null) even when there is data in the text field.

    I also know that the new row will not display (null) if the line

    [self setInfo: NSLocalizedString(@"<Your Appointment Info>", @"String to display for unset appointment info")];

    is used instead of

    [self setInfo:[NSString stringWithFormat:mad:"%@",[newDataField stringValue]]];

    In that case it will display "<Your Appointment Info>"

    Any thoughts??
     
  12. macrumors newbie

    Joined:
    Aug 4, 2010
    #12
    warning

    it gives warning alert hides instance variables

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:mad:"Fortune Cookie" message:mad:"know your fortune,crack your cookie now" delegate:self cancelButtonTitle:mad:"No thanks" otherButtonTitles:mad:"crack the cookie",nil];
    [alert show];
    [alert release];
    please tell how to fix this warning
     
  13. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #13
    Example:

    int multiply (int x, int y) {
    int result = 0;
    if (x >= 0 && y >= 0) { int result = x * y; }
    return result;
    }

    Try guessing what multiply (3, 4) will return.

    And you can't have variables or members (local, static or extern) of type NSString, only variables and members of type NSString*.


    Look in the @interface what instance variables you have. Then look what local variables you have. Notice anything?

    Good strategy is to use names for instance variables that can be clearly distinguished from other names. Makes your code a lot, lot more readable as well.
     
  14. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #14
    A non Objective-C example:

    char *p1, p2;

    What _exactly_ does this declaration do? (It should be obvious that it doesn't do what you think it does or I wouldn't have posted it, but what _does_ it mean? ). Now apply that knowledge to your example. You may try

    char *p1 = "Hello", p2 = ", world";

    and the compiler might give you a useful hint.
     
  15. macrumors member

    Joined:
    Oct 9, 2008
    #15
    This is borderline off-topic, but just in case some memory management novices are trying to learn from discussions like this one...

    Looks like a perfectly fine run-of-the-mill setter to me. I do, however, think the alternative setter you're suggesting contains a memory leak in the following lines:

    You're releasing once, but retaining twice (once implicitly in -copy, then once more explicitly). If this actually represents the setters you're using in your projects, you should review your code some time soon. :)

    (I'd also suggest switching to properties whenever possible. After all, the safest way to avoid memory leaks is not even writing code that's prone to leaking in the first place.)

    --

    EDIT: Yikes! I fell for an incredibly old thread.
     

Share This Page