How to convert a string to NSData - xamarin.ios

i have a string that I use to create a file. I now need to take that file and get it into an NSData object for sending as an email.
I'm sorry, but I haven't a clue how to get a string into an NSData object! Can someone please help me?

Use NSData.FromString (string s), unless you mean loading a file name that is stored in a string and getting the NSData of that, in which case use NSData.FromStream (new FileStream (filename, FileMode.Open));

Related

How do I convert an InputStream into a Map in Groovy?

Suppose I have an InputStream that contains JSON data, and I want to convert it to a Map(Key Value Pairs), so for example I can write that to a log file.
What is the easiest way to take the InputStream and convert it to a Map?
public String convertInputStreamToMap(InputStream isobj) {
// ???
}
I've tried converting to String which works as expected but when the data is really long the data will be incomplete. So I want some easiest way to directly convert this to Map.
Use ObjectMapper from com.fasterxml.jackson.databind to directly convert inputstream to Map:
for example:
objectMapper.readValue(is, Map.class);
there is a built-in class in groovy to parse json: groovy.json.JsonSlurper
it has parse method that accepts almost any source including InputStream
def url = 'https://httpbin.org/get'.toURL()
def json = url.withInputStream{inputStream->
new groovy.json.JsonSlurper().parse(inputStream)
}
println json.headers
and if you want to convert InputStream to String, groovy provides additional methods to work with InputStream class: https://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/InputStream.html
the following code reads the content of this InputStream using specified charset and returns it as a String.
String s = inputStream.getText("UTF-8")

Swift: Converting NSNumber into NSString (NSNumber is not a subtype of NSString)

In my app, I'm going into CoreData and grabbing an entry whose type is a Double, then I'm trying to put that value into a text field in my app.
This looks like
lengthTextField.text = lastSession.length but I'm getting the error NSNumber is not a subtype of NSString
The value of lastSession.length is 6.0 for reference. Any suggestions on how to properly put that data in my text field?
Thanks!
In core data numbers are backed by NSNumber. You can view the documentation here
In swift you can access the string representation of the number through the instance property stringValue
lengthTextField.text = lastSession.length.stringValue
You'll have to format the NSNumber's doubleValue member to a string before outputting.
let x:NSNumber = 6.0
let s:String = String(format:"%f", x.doubleValue) //formats the string to accept double/float
println(s)

How is StringWriter taking taking new value?

Can someone please explain to me how writer.toString would hold the customer data? It appears as though marshal(customer,writer) somehow modified the writer. Please see below, thanks:
Customer customer = new Customer();
customer.setId(42);
customer.setName("Bill Burke");
JAXBContext ctx = JAXBContext.newInstance(Customer.class);
StringWriter writer = new StringWriter();
ctx.createMarshaller().marshal(customer, writer);
[See here:]
String custString = writer.toString(); // how did writer suddenly take on value?
customer = (Customer)ctx.createUnmarshaller()
.unmarshal(new StringReader(custString));
StringWriter is a Writer-implementation that writes into a StringBuffer. When you call write(), you append to that StringBuffer. ToString() calls toString() on that StringBuffer and returns it.
Marshaller.marshal() just serializes the JAXB objects into XML, and writes them into the Writer (and thus into the StringBuffer). The StringReader in your second snippet then reads from the String created by StringBuffer.
I'm guessing that the reason writer is able to write the content is because the marshaller must have called a method inside the write object which changed whatever write points to. Therefore, after the marshal(), the writer references new data.
http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html (see bottom)

Type Casting for uitextfield in montouch?

I need to create the UITextField as a name of string that i'm getting from Service.
I tried the following way
string name = "Sample";
UITextField name = new UITextField();
But i'm getting already defined data Type error.
Please give me the solution.
Thank you in Advance,
You can't have two variables named the same. This is a general C# syntax issue.
Does it work if you do this:
string name = "Sample";
UITextField nameField = new UITextField();

How are AMF3 string tables built?

I am working on AMF3 format decoding. I have used inline objects and variables, however I am having problem with the String reference. Can anyone tell how String reference table is built?
Currently, I have:
var object:Object = new Object();
object.s1 = "abc";
object.s2 = object.s1;
object.s0 = "111";
byteBuffer.writeObject(object);
then I write this to a byteBuffer and finally to a file. I am wondering how String references are built?
Flex builds a table for string references. As soon as Flex encounters a string in an object, it does the following:
If the string is new:
Add the string to the table and increase the reference number.
Otherwise
add a reference to the string.

Resources