Class: NSData

Inherits:
Object show all
Defined in:
lib/ios/sugarcube-nsdata/nsdata.rb,
lib/osx/sugarcube-nsdata/nsdata.rb,
lib/cocoa/sugarcube-files/nsdata.rb,
lib/cocoa/sugarcube-nsdata/nsdata.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.read_from(path_or_url) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/cocoa/sugarcube-files/nsdata.rb', line 5

def read_from(path_or_url)
  case path_or_url
  when NSURL
    self.dataWithContentsOfURL(path_or_url)
  when NSString
    self.dataWithContentsOfFile(path_or_url)
  else
    false
  end
end

Instance Method Details

#nsimageNSImage



4
5
6
# File 'lib/osx/sugarcube-nsdata/nsdata.rb', line 4

def nsimage
  return NSImage.alloc.initWithData(self)
end

#nsstring(encoding = nil) ⇒ NSString

converts NSData into an NSString using any encoding, default is UTF8



5
6
7
8
9
10
11
12
# File 'lib/cocoa/sugarcube-nsdata/nsdata.rb', line 5

def nsstring(encoding=nil)
  if encoding
    return NSString.stringWithCString(self.bytes, encoding: encoding)
  else

    return NSString.stringWithUTF8String(self)
  end
end

#uiimage(scale = nil) ⇒ UIImage



4
5
6
7
8
9
10
# File 'lib/ios/sugarcube-nsdata/nsdata.rb', line 4

def uiimage(scale=nil)
  if scale
    return UIImage.imageWithData(self, scale:scale)
  else
    return UIImage.imageWithData(self)
  end
end

#write_to(path_or_url, atomically = true) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/cocoa/sugarcube-files/nsdata.rb', line 18

def write_to(path_or_url, atomically=true)
  case path_or_url
  when NSURL
    self.writeToURL(path_or_url, atomically: atomically)
  when NSString
    self.writeToFile(path_or_url, atomically: atomically)
  else
    false
  end
end