Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-09-2009, 09:35 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default Taggin Array Objects (Or however else possible)

Hi,

I have an NSMutableArray which is empty at first but eventually gets filled with objects (NSString) recieved through WiFi and through the users input. This array is shown in a tableView. Pretty staight forward so far...

The problem is that I need distinguish the objects im my array recieved through WiFi from the ones recieved through the users input in order to diplay them differently in the tableView.

Example:

===============
My recieved object
===============
My inputted object
===============
My recieved object
===============
My recieved object
===============
My inputted object
===============

and so on....

How can I do this? I thought about tagging them at first but im not sure if you can tag objects in an array.
Thanks alot for your help!
freshking is offline   Reply With Quote
Old 09-09-2009, 10:55 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

create two different class objects
@interface MyReceivedObject;
@interface MyInputtedObject;

You can add both to your NSMutableArray at the appropriate times. That is, when you recieve something from Wifi, you add a new MyRecievedObject initialized with all necessary info. The same goes for when you receive user input, i.e. you add a MyInputtedObject.

Now, in your tableview, in cellForRowAtIndexPath, you can check the type and do whatever

Code:
-(UITableViewCell*)cellForRow...
{
     //...deque a cell or create a new one
     //..

       id obj = [theArray objectAtIndex: indexPath.row];
      if( [obj isKindOfClass: [MyReceivedObject class]] )
      {
          //set cell text color, etc for receivedtypes
      }
      else if ( [obj isKindOfClass: [MyInputtedObject class]])
      {
           //set cell text color, etc for inputted types
      }
      

     
}
EDIT:

Alternatively, if the MyRecievedObject and MyInputtedObject contain identical information, then it maybe easier to create one object, and give it a "type".

Code:
typedef enum _ObjectType
{
   Inputted,
   Received
} ObjectType;
@interface MyObject
{
    ObjectType type;
}
 ...
@end
Now in your cellForRowAtIndexPath, you could just check the MyObject type, rather than checking isKindOfClass.

Last edited by smithdale87; 09-09-2009 at 10:58 AM.
smithdale87 is offline   Reply With Quote
Old 09-09-2009, 10:59 AM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,526
Default

I had posted something really klunky, but then when I read smithdale87's response, I liked it much better, so I am deleting mine.

Last edited by RLScott; 09-09-2009 at 11:02 AM.
RLScott is offline   Reply With Quote
Old 09-09-2009, 11:41 AM   #4 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Send a message via AIM to Dutch
Default

I hate when that happens.
Dutch is offline   Reply With Quote
Old 09-09-2009, 11:46 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

Code:
@interface ArrogantLaugh

-(void)execute;
@end

////
ArrogantLaugh* laugh = [[ArrogantLaugh alloc] init];
[laugh execute];
[laugh release];
smithdale87 is offline   Reply With Quote
Old 09-09-2009, 11:48 AM   #6 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Send a message via AIM to Dutch
Default

Code:
[[laugh owner] kill];
Dutch is offline   Reply With Quote
Old 09-09-2009, 12:37 PM   #7 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

Such violence young grasshopper... Learn to control your anger
smithdale87 is offline   Reply With Quote
Old 09-09-2009, 12:43 PM   #8 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Send a message via AIM to Dutch
Default

Quote:
Originally Posted by smithdale87 View Post
Such violence young grasshopper... Learn to control your anger
I was only kidding of course!!
Dutch is offline   Reply With Quote
Old 09-09-2009, 12:51 PM   #9 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

@end ThreadHijack
smithdale87 is offline   Reply With Quote
Old 09-09-2009, 02:29 PM   #10 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

Thank you all for your answers!

@smithdale87:

Could you explain in more detail how to create the object classes?

@interface MyReceivedObject;
@interface MyInputtedObject;

I dont know why but its not working for me.
Thank you!
freshking is offline   Reply With Quote
Old 09-09-2009, 02:42 PM   #11 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,526
Default

Quote:
Originally Posted by freshking View Post
...Could you explain in more detail how to create the object classes?

@interface MyReceivedObject;
@interface MyInputtedObject;
Wouldn't it be something like:

Code:
@interface MyReceivedObject : NSString {}
@end
@interface MyInputtedObject : NSString {}
@end
@implementation MyReceivedObject
@end
@implementation MyInputtedObject
@end
This creates two subclasses of NSString that differ only in their class name. You may even be able to leave off the implementation blocks, since they are null. I haven't tried it. But it can't hurt to put them in.

Robert Scott
Ypsilanti, Michigan
RLScott is offline   Reply With Quote
Old 09-09-2009, 03:03 PM   #12 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

Quote:
Originally Posted by RLScott View Post
Wouldn't it be something like:

Code:
@interface MyReceivedObject : NSString {}
@end
@interface MyInputtedObject : NSString {}
@end
@implementation MyReceivedObject
@end
@implementation MyInputtedObject
@end
This creates two subclasses of NSString that differ only in their class name. You may even be able to leave off the implementation blocks, since they are null. I haven't tried it. But it can't hurt to put them in.

Robert Scott
Ypsilanti, Michigan
Thants what I tried but I always get this error message:

Quote:
[Session started at 2009-09-09 20:51:02 +0200.]
GNU gdb 6.3.50-20050815 (Apple version gdb-966) (Tue Mar 10 02:43:13 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 65921.
warning: .o file "/Users/Basti/Desktop/Texter/build/Texter.build/Release-iphonesimulator/Texter.build/Objects-normal/i386/AppController.o" more recent than executable timestamp
kill
error while killing target (killing anyway): warning: error on line 1987 of "/SourceCache/gdb/gdb-966/src/gdb/macosx/macosx-nat-inferior.c" in function "macosx_kill_inferior_safe": (os/kern) failure (0x5x)
quit

The Debugger has exited with status 0.(gdb)
freshking is offline   Reply With Quote
Old 09-09-2009, 03:33 PM   #13 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

Its hard to tell you much about that error without seeing more code.

Not sure why you're getting errors trying to extend NSString. Seems odd, but I'm not at my mac right now so i cant test it out. Instead, you could do this:
Code:
typedef enum _StringType
{
     Input, Received
} StringType;
@interface MyObject: NSObject
{
    NSString* theString;
    StringType type;
    
}
@property (nonatomic, readonly) NSString* theString;
@property (readonly) StringType type;

-(id) initWithString:(NSString*) aString andType:(StringType)type;

@end

@implementation

//im sure you can figure out what goes here.
@end



//when you recieve something over wifi

MyObject* obj = [[MyObject alloc] initWithString: @"whatever was received" andType: Received ];

[myArrayOfObjects addObject: obj];
[obj release];


//when something new is input by the user
MyObject* obj = [[MyObject alloc] initWithString: @"whatever was input" andType: Input];

[myArrayOfObjects addObject: obj];
[obj release];


//in your table
-(UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
      UITableViewCell* cell;
        //... 

       MyObject* obj = [myArrayOfObject objectAtIndex:indexPath.row];
       if( obj.type == Received )
       {
         cell.textColor = [UIColor redColor];
       }
       else if( obj.type == Input )
       {
           cell.textColor = [UIColor greenColor];
       }

        cell.text = obj.theString;
}

Last edited by smithdale87; 09-09-2009 at 03:52 PM.
smithdale87 is offline   Reply With Quote
Old 09-09-2009, 04:11 PM   #14 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

Sorry to bother you again but...:

Quote:
2009-09-09 22:09:21.892 Texter[67536:20b] *** NSInvocation: warning: object 0xb880 of class 'MyObject' does not implement doesNotRecognizeSelector: -- abort
I don't know what the problem is. Im doing it exactly like you showed me!
freshking is offline   Reply With Quote
Old 09-09-2009, 04:23 PM   #15 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

did you be sure to specify that MyObject is a subclass of NSObject?
Code:
@interface MyObject: NSObject
If you did, and you still get that error, can you please post the code exactly as you're using it?
smithdale87 is offline   Reply With Quote
Old 09-09-2009, 04:33 PM   #16 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

Im probably doing something really dumb but here you go:

Quote:
typedef enum _StringType
{
Input, Received
} StringType;

@interface MyObject: NSObject
{
NSString* theString;
StringType type;

}
@property (nonatomic, readonly) NSString* theString;
@property (readonly) StringType type;

-(id) initWithString: (NSString*) aString andType: (StringType)type;

@end

@implementation MyObject

-(id) initWithString: (NSString*) aString andType: (StringType)type{

return theString;
}

@synthesize theString, type;

@end
Quote:
//when something new is input by the user
MyObject* obj = [[MyObject alloc] initWithString:input.text andType: Input];
[services addObject: obj];
[obj release];
Quote:
//when you recieve something over wifi
MyObject* obj = [[MyObject alloc] initWithString: output andType: Received ];
[services addObject: obj];
[obj release];
Quote:
MyObject* obj = [services objectAtIndex:indexPath.row];

if (obj.type == Received) {
//whatever
} else if (obj.type == Input) {
//whatever
}
freshking is offline   Reply With Quote
Old 09-09-2009, 04:42 PM   #17 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

Code:
-(id) initWithString: (NSString*) aString andType: (StringType)type{

return theString; 
}
The above should be changed to this:
Code:
@synthesize theString, type;

-(id) initWithString: (NSString*) aString andType: (StringType)_type{
   self = [super init];
   if( self != nil )
   {
      self.theString = aString;
      type = _type;
      
   }
   return self;

}
//also add this in MyObject
-(void)dealloc
{
   [theString release];
   [super dealloc];
}
Hopefully this will solve your problem, or at least get you closer to the solution.
smithdale87 is offline   Reply With Quote
Old 09-09-2009, 05:09 PM   #18 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

Thank you very much! Works fine! I found out two things:

You have the change this

Quote:
@property (nonatomic, readonly) NSString* theString;
to

Quote:
@property (nonatomic, retain) NSString* theString;
Otherwise it will not compile du to this error:

Quote:
error: object cannot be set - either readonly property or no setter found
I hope that is correct!?

===========================

And now I have a different problem. I Use a UILabel in my cell to diplay text. I resize the cell automatically to fit the text length and now im getting this error:

Quote:
2009-09-09 23:01:51.082 Texter[68905:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[MyObject sizeWithFont:constrainedToSize:lineBreakMode:]: unrecognized selector sent to instance 0xd19810'
Quote:
CGSize constraintSize;
constraintSize.width = 200.0f;
constraintSize.height = MAXFLOAT;
CGSize stringSize = [cellValue sizeWithFont: [UIFont systemFontOfSize: 12] constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
freshking is offline   Reply With Quote
Old 09-09-2009, 05:15 PM   #19 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

Quote:
You have the change this

Quote:
@property (nonatomic, readonly) NSString* theString;
to

Quote:
@property (nonatomic, retain) NSString* theString;
Otherwise it will not compile du to this error:

Quote:
error: object cannot be set - either readonly property or no setter found
I hope that is correct!?
That works fine as well.


Quote:
And now I have a different problem. I Use a UILabel in my cell to diplay text. I resize the cell automatically to fit the text length and now im getting this error:
Quote:
2009-09-09 23:01:51.082 Texter[68905:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[MyObject sizeWithFont:constrainedToSize:lineBreakMode:]: unrecognized selector sent to instance 0xd19810'
Quote:
CGSize constraintSize;
constraintSize.width = 200.0f;
constraintSize.height = MAXFLOAT;
CGSize stringSize = [cellValue sizeWithFont: [UIFont systemFontOfSize: 12] constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
Look at what the error is saying, the method -[MyObject sizeWithFont:constrainedToSize:lineBreakMode:] does not exist in the MyObject class.

I see you're calling that method here:
Code:
CGSize stringSize = [cellValue sizeWithFont: [UIFont systemFontOfSize: 12] constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
I'm assuming that cellValue is a MyObject? If so, you need to do
Code:
CGSize stringSize = [cellValue.theString sizeWithFont: [UIFont systemFontOfSize: 12] constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
smithdale87 is offline   Reply With Quote
Old 09-09-2009, 05:25 PM   #20 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

I have got to be the stupidest person alive!!!!

It works perfectly now!

Thank you very much!

I really am very greatful so please do check out my webseit and tell me if you want any promo codes for any of my apps. I would love to give you some!
freshking is offline   Reply With Quote
Old 09-09-2009, 05:36 PM   #21 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,558
Send a message via ICQ to smithdale87 Send a message via AIM to smithdale87 Send a message via Skype™ to smithdale87
Default

Quote:
I have got to be the stupidest person alive!!!!
Not necessarily stupid. The inability to interpret an error message comes from lack of exposure, so I'd say you're more ignorant than stupid. lol

Once you start learning to interpret the error messages, you'll find that, most of the time, they're quite helpful and tell you exactly what's wrong.
smithdale87 is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutablearray, nsstring, tag, wifi

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 271
20 members and 251 guests
ADY, AragornSG, Bertrand21, Dani77, Dattee, fkmtc, HDshot, iDifferent, JasonR, jimbo, macquitzon216, mer10, prchn4christ, Rudy, sacha1996, sneaky, spiderguy84, Sunny46, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:08 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0