Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 8, 2013, 02:17 AM   #1
MickeyT
macrumors member
 
Join Date: Apr 2010
Location: Newcastle, United Kingdom
What is the point of a structure?

I get basic data types, and I get classes. When dealing with data types, you're dealing with the actual data. With classes, you're only dealing with the pointer to the data.

Where do structures come into it and why are they needed? Wouldn't a class work well enough if more than one piece of data formed part of the object?

I have reached some exercises in a book I have which seeks to render a user's location on a map, and adds annotations etc. Quite a few arguments that are passed to the MKMapView or a CLLocationManager seem to be these things, and I'm just not sure I get the role they play as a holder of data in the same way I understand basic data types and classes.

Also, how are you meant to know if something is a structure - is there a naming convention or are you just meant to know from the documentation and the absence of the * when declaring them?

And finally, when passing arguments to something and those arguments sit within () brackets, is this the syntax for structures (as in structures use (), whilst classes use [] as part of the method calling syntax)? So, for example, is NSLog a structure?

Thank you
__________________
13" Macbook Pro, 2.4 GHz, 8 GB RAM; 16GB iPhone 5, 32GB iPad 3

Last edited by MickeyT; Jan 8, 2013 at 12:42 PM.
MickeyT is offline   0 Reply With Quote
Old Jan 8, 2013, 02:30 AM   #2
Menneisyys2
macrumors 601
 
Join Date: Jun 2011
1, to make it much easier to port apps from e.g. C. Socket programming, for example, makes HEAVY use of structs.

2, structure instances are much more lightweight than true class instances - there is absolutely no overhead in memory when using them, unlike with true objects.

3, they work exactly the same way as classes BUT can't incorporate methods, just data fields and have no fine-grained protection / visibility constraints / inheritance either.

(Actually, when it comes to the first and third in this list,

1, classes are generalizations of structs with additional methods inside. This is how, BTW, I teach the basics of OOP - starting with structs, explaining that classes are generally (physically) no more than structs with additional executable code in them etc.

2, how inheritance works physically (how the struct instances look like on the stack / in the heap) and the lack of restrictive inheritance resulting in type compatibility you can always depend on in both Java and Objective-C (as opposed to C++, where it's not guaranteed as C++ does support restrictive inheritance) can be very nicely shown by using structs. I just love structs when it comes to teaching OOP for C folks - they allow for explaining OOP really easy without any abstract stuff most OOP textbooks love to use (car + wheels, house + windows etc.).
)

Also see http://stackoverflow.com/questions/2...truct-vs-class for more info

Last edited by Menneisyys2; Jan 8, 2013 at 02:36 AM.
Menneisyys2 is offline   0 Reply With Quote
Old Jan 8, 2013, 02:45 AM   #3
solderguy1
macrumors member
 
Join Date: Apr 2012
Historically, C supported structures before Obj-C and C++ were created to use classes. It's not like structures and classes suddenly appeared together.
I've seen one IOS game developing book recommend using structures over classes whenever possible if you're trying for 60fps graphics. The author said classes had too much messaging overhead.
solderguy1 is offline   0 Reply With Quote
Old Jan 8, 2013, 03:05 AM   #4
MickeyT
Thread Starter
macrumors member
 
Join Date: Apr 2010
Location: Newcastle, United Kingdom
Quote:
I just love structs when it comes to teaching OOP for C folks - they allow for explaining OOP really easy without any abstract stuff most OOP textbooks love to use (car + wheels, house + windows etc.).
Yes, those sorts of examples are exactly how I picked up OOP and, to be honest, it did turn out to be understandable.

So, can I think of structs as classes without methods, and that they're the actual data rather than pointers?

I followed one particular struct (MKCoordinateRegion) through to find out what data types its contained data members were. They were also all structs until I eventually reached CLLocationDegrees, which is just a double. I don't think this is a struct, because it just says:

Code:
typedef double CLLocationDegrees;
What is the point - why not just stop at the next level up (a struct called MKCoordinateSpan) and have that contain just doubles?

----------

And thank you for the link - I'll read when I get home this evening.
__________________
13" Macbook Pro, 2.4 GHz, 8 GB RAM; 16GB iPhone 5, 32GB iPad 3
MickeyT is offline   0 Reply With Quote
Old Jan 8, 2013, 03:17 AM   #5
Menneisyys2
macrumors 601
 
Join Date: Jun 2011
Quote:
Originally Posted by MickeyT View Post
Yes, those sorts of examples are exactly how I picked up OOP and, to be honest, it did turn out to be understandable.
Yup, this is why I too use them to teach OOP for purely C (or Pascal) folks. The "abstract" approaches (the ones most OOP textbooks use) are pretty hard to grasp if you have a strong C or other knowledge of procedural programming languages.

Quote:
Originally Posted by MickeyT View Post
So, can I think of structs as classes without methods, and that they're the actual data rather than pointers?
Yup, they can be stored on the stack (as a local variable) and can be, consequently, accessed w/o using pointers. (They can also be put on the heap with the standard malloc calls too.)

Quote:
Originally Posted by MickeyT View Post
I followed one particular struct (MKCoordinateRegion) through to find out what data types its contained data members were. They were also all structs until I eventually reached CLLocationDegrees, which is just a double. I don't think this is a struct, because it just says:

Code:
typedef double CLLocationDegrees;
What is the point - why not just stop at the next level up (a struct called MKCoordinateSpan) and have that contain just doubles?.
It's more understandable this way. If you look at the declaration, you immediately know it's some kind of a coordinate. Of course, you can always use simple doubles instead of CLLocationDegrees but it won't be as instructive for people reading your source code.
Menneisyys2 is offline   0 Reply With Quote
Old Jan 8, 2013, 03:40 AM   #6
MickeyT
Thread Starter
macrumors member
 
Join Date: Apr 2010
Location: Newcastle, United Kingdom
Okay, I think I understand. And I suppose using structs inside classes is pointless because you would normally have properties where the name of the accessor method would be the giveaway as to what the basic data type was meant to represent?

So, please could I also just briefly clarify the syntax. Are these made up examples essentially equivalent, albeit one is a class and one is a struct:

Code:
MyClass *classInstance = [[MyClass alloc] initWithAnInt:anInt andADouble:aDouble];

MyStruct structData = MyStruct(anInt, aDouble);
__________________
13" Macbook Pro, 2.4 GHz, 8 GB RAM; 16GB iPhone 5, 32GB iPad 3
MickeyT is offline   0 Reply With Quote
Old Jan 8, 2013, 04:04 AM   #7
MickeyT
Thread Starter
macrumors member
 
Join Date: Apr 2010
Location: Newcastle, United Kingdom
Quote:
And I suppose using structs inside classes is pointless because you would normally have properties where the name of the accessor method would be the giveaway as to what the basic data type was meant to represent?
Actually, forget that bit. I'm not sure I agree with myself now after thinking about it......Hmmm.
__________________
13" Macbook Pro, 2.4 GHz, 8 GB RAM; 16GB iPhone 5, 32GB iPad 3
MickeyT is offline   0 Reply With Quote
Old Jan 8, 2013, 04:14 AM   #8
Menneisyys2
macrumors 601
 
Join Date: Jun 2011
Quote:
Originally Posted by MickeyT View Post
Okay, I think I understand. And I suppose using structs inside classes is pointless because you would normally have properties where the name of the accessor method would be the giveaway as to what the basic data type was meant to represent?

So, please could I also just briefly clarify the syntax. Are these made up examples essentially equivalent, albeit one is a class and one is a struct:

Code:
MyClass *classInstance = [[MyClass alloc] initWithAnInt:anInt andADouble:aDouble];

MyStruct structData = MyStruct(anInt, aDouble);
1. The latter should read

struct MyStruct structData ={anInt, aDouble};

where you can leave the leading "struct" if you typedef the struct by, say, "typedef struct GenericStruct MyStruct;"

2. structs created this way are on the stack (as opposed to being on the heap - see the alloc + init method call pair) and, therefore, should not be referenced after the current method is finished (don't assign their address to any global variable / property for later access). This is a major difference between "first-class" objects (instantiated from true classes) and struct variables.
Menneisyys2 is offline   0 Reply With Quote
Old Jan 8, 2013, 06:32 AM   #9
MickeyT
Thread Starter
macrumors member
 
Join Date: Apr 2010
Location: Newcastle, United Kingdom
Okay, thanks. I'm a bit confused then - this is an example from the programme in the book when a struct is created for use in moving and zooming the map:

Code:
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
For information, loc is a CLLocationCoordinate2D (a struct!!).

MKCoordinateRegionMakeWithDistance is followed by () containing arguments, which is why I used them in my example. But actually, I think maybe MKCoordinateRegionMakeWithDistance is a function and not a struct - am I right? If I recall correctly, MKCoordinateRegionMakeWithDistance was denoted with an "f" in the Apple documentation.

If so, what owns the function? What am I calling when I use a function (as opposed to a method which I understand belongs to either the class or the class instance)?
__________________
13" Macbook Pro, 2.4 GHz, 8 GB RAM; 16GB iPhone 5, 32GB iPad 3
MickeyT is offline   0 Reply With Quote
Old Jan 8, 2013, 06:58 AM   #10
Menneisyys2
macrumors 601
 
Join Date: Jun 2011
Quote:
Originally Posted by MickeyT View Post
But actually, I think maybe MKCoordinateRegionMakeWithDistance is a function and not a struct - am I right? If I recall correctly, MKCoordinateRegionMakeWithDistance was denoted with an "f" in the Apple documentation.
Yup, it's a function. You can't create struct variables with the () notation - there isn't anything like a constructor for pure structs.

(BTW, there are a lot of other, similar helper MK* functions in iOS / OS X programming. They generally also return struct variables.)

----------

Quote:
Originally Posted by MickeyT View Post
If so, what owns the function? What am I calling when I use a function (as opposed to a method which I understand belongs to either the class or the class instance)?
It's in the standard library of iOS - in the MapKit framework, declared in the MKGeometry.h / MKOverlayView.h headers. You just add the framework & import those headers in your app and you're set.
Menneisyys2 is offline   0 Reply With Quote
Old Jan 8, 2013, 07:06 AM   #11
MickeyT
Thread Starter
macrumors member
 
Join Date: Apr 2010
Location: Newcastle, United Kingdom
Thank you very much.

So, to close this one down for the meantime (I sense I haven't seen the last of this), for novice-level stuff that I am grappling with currently is it pretty safe to say that I can create any structs that I need using functions provided or which are returned from class methods, and that, in addition, I just need to understand the "no pointers, no methods" idea for now?

Sticking structs on the heap was something else intriguing you said, but I think I'll leave that alone for the moment...........

Again, I'm very appreciative of your help.
__________________
13" Macbook Pro, 2.4 GHz, 8 GB RAM; 16GB iPhone 5, 32GB iPad 3
MickeyT is offline   0 Reply With Quote
Old Jan 8, 2013, 08:06 AM   #12
PhoneyDeveloper
macrumors 68020
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
structs allow developers to create their own data types. Consider CGPoint and CGRect. It's hard to imagine doing computer graphics without point and rectangle data types, but these types are not fundamental to the computer language, so need to be created by the developers who developed the graphics API. You could imagine that the point and rectangle data types could be classes rather than structs, and probably in some OOP languages or graphics APIs these types are classes. On iOS Core Graphics is a C API, mostly, so these types need to be C constructs, which leaves only structs. You could also imagine that everyplace where the graphics APIs need points and rects that they just take two or four float values as parameters. That would be error prone and ugly.

typedef and enum also allow the developer to create their own data types. Creating your own data type serves mostly heuristic value but since it allows you to make your code simpler and easier to understand this is very important.
PhoneyDeveloper is offline   0 Reply With Quote
Old Jan 8, 2013, 08:55 AM   #13
ArtOfWarfare
macrumors 601
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
typedef also allows Apple to modify how the API works without breaking code that uses it. IE, they can change a typedef int to a typedef long, and your code needs not change anything as long as you were using their typedef types all along.
ArtOfWarfare is offline   0 Reply With Quote
Old Jan 8, 2013, 11:22 AM   #14
MickeyT
Thread Starter
macrumors member
 
Join Date: Apr 2010
Location: Newcastle, United Kingdom
Quote:
structs allow developers to create their own data types. Consider CGPoint and CGRect. It's hard to imagine doing computer graphics without point and rectangle data types, but these types are not fundamental to the computer language, so need to be created by the developers who developed the graphics API. You could imagine that the point and rectangle data types could be classes rather than structs, and probably in some OOP languages or graphics APIs these types are classes.
Quote:
typedef and enum also allow the developer to create their own data types.
Right, okay, that helps to make more sense. The mention of enumerators in particular has made me realise that I have perhaps been using something similar already. In Excel VBA, the options for setting certain parameters are often enumerators, usually prefixed with "xl". For example, xlLineStyle (which I think is used in changing cell borders) is enumerated into xlContinuous, xlDash, xlDashDot, etc. But these appear to be just integer values. xlContinuous is a 1, xlDash is -4145, xlDashdot a 4.

So actually, it seems like these are a bit like the CLLocationDegrees I mentioned earlier on (in that they contain just a number), and that perhaps a struct is like xlLineStyle (in that xlLineStyle effectively contains 8 variables that are just integers)? If this is in any way broadly similar, then I think I've got it because I use enumerators a lot to restrict something to certain values where there are more than 2 possibilities (I would just use a Boolean otherwise).
__________________
13" Macbook Pro, 2.4 GHz, 8 GB RAM; 16GB iPhone 5, 32GB iPad 3
MickeyT is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Tags
struct

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 06:52 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC