2013-02-28 5 views
4

String 클래스에 추가 메서드를 추가했습니다. 나중에이 메서드를 사용하고 싶지만 메서드가 잘못되었습니다.Rubymotion : 메서드 추가시 NoMethodError

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

문제가 해결되었습니다. 어떤 이유로 String 클래스를 확장해도 항상 작동하지는 않습니다. 나는 어떤 이유로 든 NanoStore가 "진짜"루비 문자열을 반환하지 않는다고 생각합니다. "String"을 "NSString"으로 대체하여 해결했습니다.

class NSString 
    def as_file_full_path 
     NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) [0].stringByAppendingPathComponent(self)  
    end 
end 
관련 문제