4

String クラスに追加のメソッドを追加しました。後でこのメソッドを使用したいのですが、no Method エラーが発生します。

class String
   def as_file_full_path
      NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)   [0].stringByAppendingPathComponent(self)    
  end
end

REPLで次のことを試すと、うまくいきます:

 (main)> "image.jpg".as_full_path
  => "/Users/user/Library/Application Support/iPhone Simulator/6.1/Applications/30D186A9-B1C7-4377-AE91-0D14BD3B4A6D/Documents/image.jpg"

しかし、モデル クラスでメソッドを呼び出すと、もう機能しません。

 (main)> w = Word.first
  => #<Word:0x94d7df0>
 (main)> w.filename
  => "2C86A58A-A92A-4A0F-B26C-0F5F583E142C.caf"
 (main)> w.filename.class
  => String
 (main)> w.filename.as_full_path
 2013-02-28 09:17:55.935 project[74283:c07] undefined method `as_full_path' for "2C86A58A-A92A-4A0F-B26C-0F5F583E142C.caf":String (NoMethodError)
 => NoMethodError: undefined method `as_full_path' for "2C86A58A-A92A-4A0F-B26C-0F5F583E142C.caf":String

モデルは NanoStore::Model を使用して実装されます。

編集:

モデルから返された文字列を複製すると、追加されたメソッド存在します。

w.filename.dup.as_full_path
=> "/Users/user/Library/Application Support/iPhone Simulator/6.1/Applications/30D186A9-B1C7-4377-AE91-0D14BD3B4A6D/Documents/2C86A58A-A92A-4A0F-B26C-0F5F583E142C.caf"
4

1 に答える 1

4

問題が解決しました!何らかの理由で、Stringクラスの拡張が常に機能するとは限りません。NanoStoreは、何らかの理由で「実際の」ルビー文字列を返さないと思います。「String」を「NSString」に置き換えることで解決しました。

class NSString 
   def as_file_full_path
       NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)   [0].stringByAppendingPathComponent(self)    
   end
end
于 2013-02-28T11:09:58.960 に答える