How to get object from NSSet to NSString

Discussion in 'iOS Programming' started by Fuzej, Jul 24, 2012.

  1. Fuzej macrumors newbie

    Joined:
    Jul 20, 2012
    #1
    in TableView.m:

    Code:
    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
    {
    
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NSString *selectedCell = cell.textLabel.text;
        NSLog(@"Toon mij het extra venster om details weer te geven/aan te passen van: %@", selectedCell);
        
        if(buttonPushed == @"Goals"){
            
            GoalDetailViewController *goalDetail = [[GoalDetailViewController alloc] initWithNibName:@"GoalDetailViewController" bundle:[NSBundle mainBundle]];
            goalDetail.context = context;
            goalDetail.title = goalDetail.goal.goalNaam;
            
            
            Goal *goal = [arr objectAtIndex:indexPath.row];
            goalDetail.goal = goal;
            
            [self.navigationController pushViewController:goalDetail animated:YES];
    
    When I tap the accessorybutton, I want a detailview to be loaded with the data from the selected cell.
    Loading the attributes from the Goal entity is not a problem

    Code:
    descriptionField.text = goal.goalDescription;
    nameTextField.text = goal.goalName;
    but I want to load the relationships too..
    But how do I get an object out of the NSSet ?
    And how can I turn it into a NSString?
    Is this possible in the same way as I load the description and name attributes? Or do I need a fetch request ?
     
  2. Fuzej, Jul 25, 2012
    Last edited by a moderator: Jul 26, 2012

    Fuzej thread starter macrumors newbie

    Joined:
    Jul 20, 2012
    #2
    Please tell me if my question is not clear ...Really want an answer on this one.
     
  3. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    Your question is not clear.

    What set are you talking about?

    NSSet objects are not usually used in the data source for a table view because sets are "unordered collections". The items in a set are not saved in any particular order.

    You need to explain your data structures, where you are using an NSSet and what you are using it for. Then you need to explain what you are trying to do with that set. Only then will we be able to help you.
     
  4. Fuzej, Jul 25, 2012
    Last edited by a moderator: Jul 25, 2012

    Fuzej thread starter macrumors newbie

    Joined:
    Jul 20, 2012
    #4
    in Goal.h

    Code:
    @property (nonatomic, retain) NSString * goalBeschrijving;
    @property (nonatomic, retain) NSString * goalId;
    @property (nonatomic, retain) NSString * goalNaam;
    @property (nonatomic, retain) NSSet *responsibility;
    
    - (void)addResponsibilityObject:(Actor *)value;
    - (void)removeResponsibilityObject:(Actor *)value;
    - (void)addResponsibility:(NSSet *)values;
    - (void)removeResponsibility:(NSSet *)values;

    in TableView.m (can be seen as a MasterView)


    Code:
    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
    {
    
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NSString *selectedCell = cell.textLabel.text;
        NSLog(@"Toon mij het extra venster om details weer te geven/aan te passen van: %@", selectedCell);
        
        if(buttonPushed == @"Goal"){
            
            GoalDetailViewController *goalDetail = [[GoalDetailViewController alloc] initWithNibName:@"GoalDetailViewController" bundle:[NSBundle mainBundle]];
            
            goalDetail.context = context;
            goalDetail.title = goalDetail.goal.goalNaam;
            
            Goal *goal = [arr objectAtIndex:indexPath.row];
            goalDetail.goal = goal;
            
            [self.navigationController pushViewController:goalDetail animated:YES];
    in GoalDetailViewController.m (DetailView)

    Code:
    -(void)viewWillAppear:(BOOL)animated{
        
       
        [self setTitle:goal.goalNaam];
        beschrijvingField.text = goal.goalBeschrijving;
        nameTextField.text = goal.goalNaam;
       
        if([goal.responsibility count] > 0)
            {
                actoren = [[NSArray alloc] init];
        
                actoren  =  [goal.responsibility allObjects]  ;
               responsibleField.text = [actoren objectAtIndex:0];
    With the last line of code, I get an error, something which is normal because I want to put an object into a textfield.. that's where's my problem...

    Responsibility is the relationship between an Actor-entity and a Goal-entity (using Core Data).
    In general, when a relationship is added to a NSSet, (in this case a Responsibility of an Actor over a Goal) how can i retrieve the corresponding Actor, using a Goal object..
    I hope my question is clear now
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    What error? Compile-time or run-time? Etc.
     
  6. Fuzej thread starter macrumors newbie

    Joined:
    Jul 20, 2012
    #6
    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Actor _isNaturallyRTL]: unrecognized selector sent to instance 0x6b36bf0'
     
  7. chown33, Jul 26, 2012
    Last edited: Jul 26, 2012

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Only a text string can go into a textfield. Please explain how an Actor object is a text string, and can go into a textfield.

    Analogy: A 1-liter glass jar can contain fruit. Cherries are fruit. Cherries can go into the jar. This does not mean you can chop down a cherry tree and put the entire tree into the jar.

    Something you get from the tree can go into the jar. Likewise, something you get from the Actor object can go into the textfield. What is it? A name? A description? Something else?

    Saying you want the entire Actor object in the textfield is nonsense, in the same way that putting an entire cherry tree into a 1-liter jar is nonsense.


    This question has nothing to do with your problem. Please explain how getting a Goal object is related to putting a text string into a textfield. Is there something you get from a Goal object that you want to go into the textfield? What? Its name? Something else?
     
  8. Fuzej thread starter macrumors newbie

    Joined:
    Jul 20, 2012
    #8
    That's where's my problem .. I want the name of Actor (actorNaam) into the textfield. My title: How to get an object from a NSSet into a NSString ..

    In the same way I put the name (goalNaam) and a description (goalBeschrijving) into a textfield, I want to put the name of the actor (actorNaam) that's connected with a relationship, Responsibility with a Goal, into a textfield ...

    Like this:
    beschrijvingField.text = goal.goalBeschrijving;
    nameTextField.text = goal.goalNaam;




    This is the code where I add the Goal and the Responsible Actor:

    Code:
        Goal *addgoal = (Goal*)[NSEntityDescription insertNewObjectForEntityForName:@"Goal" inManagedObjectContext:context];
            
            addgoal.goalNaam = nameTextField.text;
            addgoal.goalId = idField.text;
            addgoal.goalBeschrijving = beschrijvingField.text;
           
            Actor *actor = (Actor*)[NSEntityDescription insertNewObjectForEntityForName:@"Actor" inManagedObjectContext:context];
             
            actor.actorNaam = responsibleField.text;
            
            NSLog(@"Actor %@ toegevoegd", responsibleField.text);
            
            [addgoal addResponsibilityObject:actor];
    My first question: is this the right way to add it?
    My second question: how can i retrieve the actorNaam (name of the actor, NSString) from the NSSet (responsibility)?
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Post the code that actually adds objects to the NSSet named 'responsibility'. You haven't shown any code that adds objects to any NSSet, so how are we supposed to know what types of objects are in the NSSet? All you've posted is this:
    Code:
            [addgoal addResponsibilityObject:actor];
    
    You haven't shown any implementation of addResponsibilityObject: .


    What are the types of objects in the NSSet named 'responsibility'? If the type is Actor, then show the @interface of that class. If you intend to add the name of an Actor to a textfield, then how would we know how to get its name? We don't know what its properties or instance variables are.

    If the NSSet contains Actor objects, and there is a method or property that returns its name as NSString, then it's a simple matter of following one object (the NSSet) to another object (the Actor) to the object that goes into the textfield (an NSString). You were able to do that for the Goal object, so the same logic applies.

    If the problem is you don't know how to get the correct Actor object from the NSSet, then you should explain why you decided to use NSSet. A different collection class might be more appropriate. If you've chosen the wrong collection class, it will be difficult or impossible to get it working effectively.
     
  10. Fuzej thread starter macrumors newbie

    Joined:
    Jul 20, 2012
    #10
    I thought an autogenerated accessor (CoreDataGeneratedAccessors) didn't require an implementation, .. so it does ?

    I've got no method that returns its name as NSString .. I don't know how to do that, that's my main question.


    Additional information:
    In my masterview, when i call my detailview
    Code:
            GoalDetailViewController *goalDetail = [[GoalDetailViewController alloc] initWithNibName:@"GoalDetailViewController" bundle:[NSBundle mainBundle]];
            
            goalDetail.context = context;
            goalDetail.title = goalDetail.goal.goalNaam;
            
            Goal *goal = [arr objectAtIndex:indexPath.row];
            goalDetail.goal = goal;
    
     
  11. phr0ze macrumors 6502a

    Joined:
    Jun 14, 2012
    Location:
    Columbia, MD
    #11
    I've never coded Objective C yet. So don't shoot me, but is this your answer?

    Code:
    -(void)viewWillAppear:(BOOL)animated{
        
       
        [self setTitle:goal.goalNaam];
        beschrijvingField.text = goal.goalBeschrijving;
        nameTextField.text = goal.goalNaam;
       
        if([goal.responsibility count] > 0)
            {
                actoren = [[NSArray alloc] init];
        
                actoren  =  [goal.responsibility allObjects];
                Actor *actor = (Actor*)[actoren objectAtIndex:0];
               responsibleField.text = actor.actorNaam;
    
     
  12. Fuzej thread starter macrumors newbie

    Joined:
    Jul 20, 2012
    #12
    This is indeed possibly the answer I was looking for. Thanks for the effort :)
     

Share This Page