English 中文(简体)
目标-C - 在一个类别但返回国家
原标题:Objective-C – Doing NSMutableArray operations in a class but return NSArray

我对一点混淆不清的称号感到担忧,但我要说,我有一个交错的班子。

@interface Class : NSObject {

    NSArray *items;
}

@property (nonatomic, retain) NSArray *items;

@end

在缩略语中,我实际上希望使用<代码>NSMutableArray增加一些业务,然后使用该类别的人应当能够检索数据,作为<代码>。 NSArray。

最佳回答

申报表阵列作为例变数,宣布该财产为仅读的NSArray和超标者。

@interface SomeClass : NSObject {

    NSMutableArray *mutableItems;
}

@property (nonatomic, readonly) NSArray *items;

@end

@implementation SomeClass

- (NSArray *)items {
    return [NSArray arrayWithArray:mutableItems];
}

@end
问题回答

没有理由不从宣布返回国家安全局的方法返回国家安全局。

如果你不希望用户改变你能够这样做的阵列:

@interface Class : NSObject 
- (NSArray*) items;
@end

@implementation Class
{
   NSMutableArray *items;
}

// Allocate  items  somewhere in your init method

- (NSArray*)items
{
  return items;
}

@end

你们不能将财产定义为贵方的变数,而只能将变数定义为:

@interface Class : NSObject {
    NSMutableArray *items;
}

并将其用作<条码>。 NSMutable Array:

- (id)init {
    if ((self == [super init])) {
        items = [[NSMutableArray alloc] init]; // Don t forget to release me!
    } 
}

但随后,在您的接口中,界定了一种回归<代码>的易碎方法。 NSArray:

- (NSArray *)items;

And implement it as such:

- (NSArray *)items {
    return [NSArray arrayWithArray:items];
}




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签