Reputation: 443
Few years back, I had developed an iPhone game with Xcode 4.2. Currently, when I try to open the same project in Xcode version 7.2, it is showing the following errors:
Code snippet:
Here “Node” is an interface created by me to implement linked list data structure.
- (id) initList:(int )total
{
self = [super init];
if (self)
{
totalNodes = total;
headerNode = malloc(totalNodes*sizeof(Node)); // ERROR: Application of ‘sizeof’ to interface ‘Node’ is not supported on this architecture and platform.
for(int node=0;node<totalNodes;node++)
{
headerNode->position = CGPointMake(0.0,0.0);
headerNode[node].life = 0.0; **// ERROR: Expected method to read array element not found on the object of type ‘Node *’**
headerNode[node].lifeMax = 0.0;
headerNode[node].next = NULL;
headerNode[node].previous = NULL;
}
}
return self;
}
Node.h:
@interface Node : NSObject
{
@public
CGPoint position;
CGPoint startPosition;
CGSize size;
float life;
float lifeMax;
Node *previous;
Node *next;
}
- (id)initNode:(CGPoint )_position: (CGSize)_size: (float )_life: (float )_lifeMax;
- (void)removeNode;
@end
Node.m:
#import "Node.h"
@implementation Node
- (id)initNode:(CGPoint )_position: (CGSize)_size: (float )_life: (float )_lifeMax
{
self = [super init];
if (self)
{
startPosition = position = _position;
size = _size;
life = _life;
lifeMax = _lifeMax;
next = NULL;
previous = NULL;
return self;
}
return nil;
}
- (void)removeNode
{
if(self->next != NULL)
{
self->next->previous = self->previous;
self->previous->next = self->next;
[self release];
}
else
{
self->previous->next = NULL;
[self release];
}
}
- (void)dealloc
{
// NSLog(@"dealloc NODE STARTED **********************");
[super dealloc];
// NSLog(@"dealloc NODE ENDED **********************");
}
@end
I have the following question:
Are the above issues generated due to new Xcode 7.2 (i.e. new iOS SDK)? If yes, will it be better to install Xcode 4.2 or is there any other solution to fix these issues?
I’d appreciate any suggestions on this issues. Thank you in advance.
Upvotes: 0
Views: 494
Reputation: 90571
You can't allocate Objective-C objects in bulk like you're trying to do. You must use +alloc
for each object.
In the modern API, the size of an instance of a class is not decided at build time. sizeof
is a build-time calculation. So, you can't use sizeof
to determine the size of an instance. This is because the non-fragile ABI allows for changes in the size of the superclass without recompiling subclasses. It will almost certainly not happen, but NSObject
could change size in some release of the frameworks and that would mean that your Node
class would change size when run on such a release.
You could use class_getInstanceSize()
, but you still aren't allowed to bulk allocate instances like you're trying to do. +alloc
does additional work. Setting the instance's isa
"pointer" is one such thing it does and you're failing to do. (I put "pointer" in quotes because the modern ABI doesn't necessarily use a real pointer for isa
.)
The second error, "Expected method to read array element not found on the object of type ‘Node *’", is because applying the []
operator to an object pointer does not index into an array. It attempts to treat that object like an Cocoa-array-like object (as opposed to a C-style array) and invoke its -objectAtIndexedSubscript:
method.
Note that the inability to apply []
to an object pointer and get C-array-like behavior has the same root cause as the sizeof
restriction. The compiler would have to know the size of an element of that array to do the pointer arithmetic implicit in the array indexing operation, but it can't.
Upvotes: 1