0

このコードのほとんどは、RubyMotionLocationsサンプルから直接派生しています。

単純な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

コントローラ内の表示メソッドにアクセスできないようです:

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

私はこの例外を受け取り続けます:

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

TextクラスとTextStoreクラス(モデル)にさまざまな変更を加えてみました。これまでのところ、この問題を解決したものはありません。私はAppleのドキュメントをオンラインで調べましたが、そこに手がかりは見つかりませんでした。

私はメインプロパティを使用してそれを回避しました。誰かが私がこの振る舞いを見ている理由を理解するのを手伝ってくれることを願っています。

4

1 に答える 1

2

どこにも文書化されているものは見つかりませんがdisplay、RubyMotionのほぼすべてのオブジェクトに対するプライベートメソッドのようです。display属性を指定しない限り、完全に空白のクラスでも例外がスローされます。

(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

私の推測では、NSManagedObjectの動作方法では、管理対象のオブジェクトにdisplay属性があることを最初は認識していないため、代わりにプライベートメソッドに関するエラーがスローされます。これを回避する方法があるかもしれませんが、これらのプライベートメソッドと競合する変数名を持つことは避けたいと思います。

于 2012-05-11T13:52:52.973 に答える