• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: newbie confused by obj-c syntax
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: newbie confused by obj-c syntax


  • Subject: Re: newbie confused by obj-c syntax
  • From: Jyrki Wahlstedt <email@hidden>
  • Date: Tue, 15 Mar 2005 15:18:38 +0200


On 15.3.2005, at 10:54, Daniel Child wrote:

Hi All,

I am trying to use my own homegrown array of a class I created, called StrokeDescription. This class has two member variables: desc (description) and strokeType.

Now I want to have a class that stores an array of these StrokeType objects. Somehow this didn't seem so hard in Java. I am getting hung up in Objective-C syntax and was hoping you could help out.

Three questions:
1) Is it better to always use NSArray and NSMutableArray classes, rather than create your own classes of homegrown class objects.
This is almost always the case, as those classes are tested extensively.
2) How do you return an array (and not lose it).
This depends on, whether you are dealing with a class attribute or creating an array in the function. But anyway, the return type would be (NSArray *).
3) Compared to Java, the declarations look wrong to me, even though they generated less errors than when I attempted something Java-like. Are these really OK?
The idioms in Objective-C and Cocoa are a bit different from Java. This is one thing to be learned (there are not so many of them).

Here's what I have:

#import <Foundation/Foundation.h>
#import "StrokeDescription.h"

@interface StrokesList : NSObject {
  int numInList;
  StrokeDescription *knownStrokes[100];
This would be NSMutableArray * (you have to create it = alloc+init). Later you just add objects into this and can ask the size of it.
}
- (void)addStrokeDesc:(StrokeDescription *)sd;
- (StrokeDescription *[])getStrokesOfType:(int)st;
This would be an array (NSArray *) containing objects of type StrokeDescription?
- (int[])getStrokeTypesForDescription:(NSString *)d;
@end

#import "StrokesList.h"
#import "StrokeDescription.h"

@implementation StrokesList
- (void)addStrokeDesc:(StrokeDescription*)sd; {
  knownStrokes[numInList] = sd;
  numInList++;
}

- (StrokeDescription *[])getStrokesOfType:(int)st {
StrokeDescription *matchingType[100];
int i, found = 0;
for (i = 0; i < numInList; i++) {
if ([knownStrokes[i] type] == st) { // if it matches the stroke type parameter
matchingType[found] = knownStrokes[i]; // add to the array of matching stroke types
}
} // end for
return matchingType; // return the array THIS LINE DOES NOT WORK
// also error about returning a local variable (I will lose this?)
}


- (int[])getStrokeTypesWithDescription:(NSString *)d {
// CONFLICTING TYPE WARNING EVEN THOUGH THE h FILE HAS THE SAME LINE OF CODE
// ANOTHER WARNING ABOUT DECLARED AS FUNCTION RETURNING AN ARRAY
int i = 0, found = 0;
int types[10];
NSString *thisDesc;
for (i = 0; i < numInList; i++) {
thisDesc = [knownStrokes[i] desc];
if ([thisDesc isEqualToString: d]) {
types[found] = [knownStrokes[i] type];
found++;
}
}
return *types;
}
@end


At looking, what you are doing, you could use NSMutableDictionary. That would allow you to use the types as keys and strokes in arrays as the dependent objects.


!
! Jyrki Wahlstedt
! Tinatie 3 A 2 mob. +358-40-502 0164
! FI-00440 Helsinki
!
! Our life is no dream; but it ought to become one and perhaps will.
! PGP key ID: 0x139CC386 fingerprint: F355 B46F 026C B8C1 89C0 A780 6366 EFD9 139C C386


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >newbie confused by obj-c syntax (From: Daniel Child <email@hidden>)

  • Prev by Date: Re: NSNumber & C data types
  • Next by Date: Responder chain difficulty
  • Previous by thread: Re: newbie confused by obj-c syntax
  • Next by thread: newbie confused by obj-c syntax
  • Index(es):
    • Date
    • Thread