English 中文(简体)
进口
原标题:accessor-Objective C

我是目标C的新鲜事。

I need to know how to access the instance variable using the accessors. I could able to access integer variables but not the character variable which i have declared here. Please correct if there is any wrong in this code below.

#define SIZE = 4096

 @interface data : NSObject

  {

    unsigned char id[SIZE];
  }


  @property(readwrite)unsigned char id[SIZE];

  @end

  @implementation data

  @synthesize unsigned char id[SIZE];

  @end

  main.m

  someClass* classPointer = [[someClass alloc]init];

  data* dt = [[data alloc]init];

  [classPointer createMessage:data.id];
最佳回答

为什么不使用<代码>NSString或NSData ,例如,变数而不是阵列? 例如:

@interface Foo2 : NSObject
{
    NSString *_dataId;
}

@property (nonatomic, retain) NSString *dataId;

@end


@implementation Foo2

@synthesize dataId = _dataId;

@end

否则,你就不得不做这样的事情:

#define DATA_ID_SIZE 4096

@interface Foo : NSObject
{
    char _dataID[DATA_ID_SIZE];
}

@property (nonatomic, assign) const char *dataID;

@end

@implementation Foo

// Returns a copy of the internal array of chars.
- (const char *)dataID
{
    size_t length = strlen(_dataID);
    // Dynamically allocate an array of chars to return.
    char *buf = malloc(length);
    // Copy the values from the internal array.
    memcpy(buf, _dataID, length);

    return buf;
}

- (void)setDataID:(const char *)dataID
{
    // Copy provided chars into the internal array.
    memcpy(_dataID, dataID, DATA_ID_SIZE);
    // To be on the safe side, copy null character to the last element.
    _dataID[DATA_ID_SIZE - 1] =   ;
}

这样,<代码>id就成为目标C中的一种数据类型,因此最好不要将它作为变称。

问题回答

页: 1 因此,它只是 n。

同样,在另一方面,你必须释放你所占领的物体。

如何管理记忆? <代码>C[size]是一个阵列,其大小将按类别成员大小计算,但以同样方式申报的财产将改为/标准pointer,而不是数据! 我建议您使用NSData*(或NSMutableData),而不是C-array,而是Obj-C的首选方式。





相关问题
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 ...

热门标签