Using struct with Objective-C methods
NickName:DuskFall Ask DateTime:2012-05-05T22:38:54

Using struct with Objective-C methods

I'm writing a simple game, and thought it would be much easier to use structures. However, I can't declare methods that need the structs.

How could I use a struct as an argument to an Objective-C method and get an object of the struct returned?

//my structure in the .h file
struct Entity
{
  int entityX;
  int entityY;
  int entityLength;
  int entityWidth;
  int entityType;
  bool isDead;
};

//And the methods i'm trying to use
-(BOOL)detectCollisionBetweenEntity:Entity ent1 andEntity:Entity ent2;

-(struct Entity)createEntityWithX:int newEntityX andY:int newEntityY, withType:int newEntityType withWidth:int newEntityWidth andLength:int newEntityLength;

Copyright Notice:Content Author:「DuskFall」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/10462880/using-struct-with-objective-c-methods

Answers
cobbal 2012-05-05T16:04:49

You can use structs exactly like you would expect, your problem seems to be with the syntax of methods:\n\nstruct Entity\n{\n int entityX;\n int entityY;\n int entityLength;\n int entityWidth;\n int entityType;\n bool isDead;\n};\n\n//And the methods i'm trying to use\n-(BOOL)detectCollisionBetweenEntity:(struct Entity) ent1 andEntity:(struct Entity) ent2;\n\n-(struct Entity)createEntityWithX:(int) newEntityX andY:(int) newEntityY withType:(int) newEntityType withWidth:(int) newEntityWidth andLength:(int) newEntityLength;\n\n\nTypes in methods need to be in parens, and you have to refer to struct Entity instead of Entity unless you typedef (in plain Objective-C, Objective-C++ might let you do it)",


More about “Using struct with Objective-C methods” related questions

Using struct with Objective-C methods

I'm writing a simple game, and thought it would be much easier to use structures. However, I can't declare methods that need the structs. How could I use a struct as an argument to an Objective-C ...

Show Detail

Objective-C and Vector3 struct

I am pretty new to Objective-C (so far I was working with .NET and mainly with ANSI C). So I am interssted in following: In .NET I have: struct Vector { float X, float Y ... float Length() .. } ...

Show Detail

Struct inheritance in Objective-C?

Is there anything similar to struct inheritance in Objective-C? For example, C++ allows me to do something like: struct animal { int weight; string name; } struct dog : animal { string

Show Detail

How are instance variables and methods stored in an Objective-C 2.0 object?

In the legacy version of Objective-C, an objc_class struct is implemented like this: struct objc_class { Class isa; Class super_class; const char *name; long version; long info...

Show Detail

Accessing objective-c Struct from Swift

I'm working on an ios application that has a mix of swift and obj-c code. One of my obj-c model classes defines a struct containing strings to assist in converting to a dictionary and back. I have ...

Show Detail

Prevent takeRetainedValue or takeUnretainedValue when using an Objective-C Struct

I'm using this approach to keep my string constants together. Using the same example from that post: MONExtResult.h struct MONExtResultStruct { __unsafe_unretained NSString * const AppID;

Show Detail

Using a Swift extension method in Objective-C

I have an objective-c project which includes a swift file. I've created the bridging header and imported the ProjectSwift.h into the main file etc. I am trying to use methods from the extensions ...

Show Detail

How to use Swift struct in Objective-C

Simply I have a struct that stores the application constants as below: struct Constant { static let ParseApplicationId = "xxx" static let ParseClientKey = "xxx" static var AppGreenCo...

Show Detail

How to use Swift struct in Objective-C

Simply I have a struct that stores the application constants as below: struct Constant { static let ParseApplicationId = "xxx" static let ParseClientKey = "xxx" static var AppGreenCo...

Show Detail

How to use Swift struct in Objective-C

Simply I have a struct that stores the application constants as below: struct Constant { static let ParseApplicationId = "xxx" static let ParseClientKey = "xxx" static var AppGreenCo...

Show Detail