Getting NoMethodError when accessing NSManagedObject NSString in controller

StackOverflow https://stackoverflow.com/questions/10545398

  •  07-06-2021
  •  | 
  •  

Domanda

Most of this code is derived directly from the RubyMotion Locations sample.

I defined a simple NSManagedObject:

class Text < NSManagedObject
  def self.entity
    @entity ||= begin
      # Create the entity for our Text class. The entity has 2 properties. 
      # CoreData will appropriately define accessor methods for the properties.
      entity = NSEntityDescription.alloc.init
      entity.name = 'Text'
      entity.managedObjectClassName = 'Text'
      entity.properties = ['main', NSStringAttributeType,'display',NSStringAttributeType].each_slice(2).map do |name, type|
            property = NSAttributeDescription.alloc.init
            property.name = name
            property.attributeType = type
            property.optional = false
            property
          end
      entity
    end
  end 
end

I cannot seem to access the display method inside my controller:

def tableView(tableView, cellForRowAtIndexPath:indexPath)
    cell = tableView.dequeueReusableCellWithIdentifier(CellID) || UITableViewCell.alloc.initWithStyle(UITableViewCellStyleSubtitle, reuseIdentifier:CellID)
    text = TextStore.shared.texts[indexPath.row]

    cell.textLabel.text = text.display
    cell.detailTextLabel.text = text.main[0,10] + "...."
    cell
  end

I keep getting this exception:

Terminating app due to uncaught exception 'NoMethodError', reason: 'text_controller.rb:40:in `tableView:cellForRowAtIndexPath:': private method `display' called for #<Text_Text_:0x8d787a0> (NoMethodError)

I've tried making a variety of changes to the Text class and TextStore class (model). Nothing has resolved this issue so far. I've done some research in Apple's documentation online, but have not found any clues there.

I've worked around it by using the main property. I'm hoping someone can help me understand why I'm seeing this behavior.

È stato utile?

Soluzione

Though I can't find it documented anywhere, it seems that display is a private method on just about every object in RubyMotion. Even a completely blank class throws an exception on this, unless you specify a display attribute:

(main)>> class Foo; end
=> nil
(main)>> f = Foo.new
=> #<Foo:0x8ee2810>
(main)>> f.display
=> #<NoMethodError: private method `display' called for #<Foo:0x8ee2810>>

(main)>> class Foo; attr_accessor :display; end
=> nil
(main)>> f = Foo.new
=> #<Foo:0xa572040>
(main)>> f.display
=> nil

My guess is that in the way NSManagedObject works, it doesn't initially know that the object being managed has a display attribute, so it throws the error about the private method instead. While there may be a way to work around this, I would just avoid having variable names that conflict with these private methods.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top