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-base64/nsdata.rb,
lib/cocoa/sugarcube-nsdata/nsdata.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_base64(base64_data) ⇒ Object

factory method to convert a base64 string into NSData.



12
13
14
# File 'lib/cocoa/sugarcube-base64/nsdata.rb', line 12

def self.from_base64(base64_data)
  NSData.alloc.initWithBase64EncodedData(base64_data, options: 0)
end

.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

Returns:



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

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

#nsstring(encoding = NSUTF8StringEncoding) ⇒ NSString

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

Returns:



5
6
7
# File 'lib/cocoa/sugarcube-nsdata/nsdata.rb', line 5

def nsstring(encoding=NSUTF8StringEncoding)
  return NSString.alloc.initWithData(self, encoding: encoding)
end

#to_base64Object

TODO:

Support NSDataBase64EncodingOptions options - easy to add, but I want

converts an NSData instance into a base64 string. to add specs, too, and a hash format (e.g. line_length: 64 => NSDataBase64Encoding64CharacterLineLength).



7
8
9
# File 'lib/cocoa/sugarcube-base64/nsdata.rb', line 7

def to_base64
  self.base64EncodedStringWithOptions(0)
end

#uiimage(scale = nil) ⇒ UIImage

Returns:



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