Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 741089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:35:31+00:00 2026-05-14T08:35:31+00:00

I hope to add objects to a NSMutableArray myArray, The NSMutableArray is the array

  • 0

I hope to add objects to a NSMutableArray “myArray”, The NSMutableArray is the array for FileObj which has a NSString property “fileName”

#import <UIKit/UIKit.h>


@interface FileObj :  NSObject  {

    NSString *fileName;


}


-(void) setfileName:(NSString *)s ;
-(NSString *) getfileName ;


@end

//
//  File.m//

#import "File.h"


@implementation FileObj
 -(void) setfileName:(NSString *)s ;
{
    fileName=s;
}
-(NSString *) getfileName ;
{
    return fileName;
}
@end

I initialize the myArray here:

NSMutableArray *temarray;
temarray=[[NSMutableArray alloc] init];
self.myArray=temarray;
[temarray release];

the codes to add object to myArray

FileObj *newobj=[[FileObj alloc]init ];
NSString  *fieldValue2 = [[NSString alloc]   initWithUTF8String:@"aaaa"];
[newobj setfileName:fieldValue2];



[myArray addObject:newobj];

[fieldValue2 release]; //**if I enabled the line, it will cause crash**
                       //**if I disable the line, it will cause memory leak**


[newobj release];

Welcome any comment

Thanks

interdev

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:35:32+00:00Added an answer on May 14, 2026 at 8:35 am

    First you should look into ObjC naming conventions. There is no -get methods in ObjC. It’s also a good idea to prefix your classes with your own 2 letters (like NS).

    Your setter value assignment is invalid and the NSString initialization unnecessary.
    I would strongly recommend introductory material to you!

    @interface MYFileObject : NSObject {
    
        NSString *_fileName;
    }
    
    - (void)setFileName:(NSString *)theString;
    - (NSString *)fileName;
    
    @end
    

    and the implementation

    @implementation MYFileObject
    
    - (void)setFileName:(NSString *)theString {
        [_fileName release];
        _fileName = [theString copy];
    }
    
    - (NSString *)fileName {
        return [[_fileName copy] autorelease];
    }
    
    - (void)dealloc {
        [_fileName release];
        [super dealloc];
    }
    
    @end
    

    You would add an object like this…

    NSMutableArray *myAry = [[NSMutableArray alloc] init];
    MYFileObject *obj = [[MYFileObject alloc] init];
    [obj setFileName:@"thefilename.txt"];
    
    [myAry addObject:obj];
    [obj release];
    

    I would recommend using properties instead of defining your own getters/setters.
    You could also use the NSMutableArrays’ designated initializers for fast array creation.

    Look here for how to use properties: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am designing an app which tracks data on Game objects. Each Game has
I hope to store MYFileObj to NSMutableArray (fileArray) and display data on an UITavleView(tableview).
I have an object which contains quite a few other objects in an IList
I am trying to build an array that contains arrays of dictionary objects in
I tried to add objects to the content vector, and use show() on all
I have a strange situation which I hope someone can shed some light on.
Gamemaker is a neat tool which allows for some good RAD. However it has
I have a UITableView whose datasource is a NSMutableArray . The array consists of
Possible Duplicate: Cocoa Memory Management NSArray with Objects I have an NSMutableArray filled with
I'm trying to create a class that will allow me to add/multiply/divide objects of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.