How to reverse NSObject.FromObject back to a regular C# type - monomac

What is the reverse of NSObject.FromObject, e.g., getting a regular C# object back out of an NSObject. Understandably, this could only work for simple types.
Update. Let's say I even know the type - but not before runtime. I tried
Convert.ChangeType (value, info.PropertyType, null)
but i get a message that "Value is not a convertible object: MonoMac.Foundation.NSString to System.String".
Update 2. How FromObject works:
using MonoMac.ObjCRuntime;
using System;
using System.Drawing;
public static NSObject FromObject (object obj)
{
NSObject result;
if (obj == null)
{
result = NSNull.Null;
}
else
{
Type type = obj.GetType ();
if (type == typeof(NSObject) || type.IsSubclassOf (typeof(NSObject)))
{
result = (NSObject)obj;
}
else
{
switch (Type.GetTypeCode (type))
{
case 3:
result = new NSNumber ((bool)obj);
return result;
case 4:
result = new NSNumber ((ushort)((char)obj));
return result;
case 5:
result = new NSNumber ((sbyte)obj);
return result;
case 6:
result = new NSNumber ((byte)obj);
return result;
case 7:
result = new NSNumber ((short)obj);
return result;
case 8:
result = new NSNumber ((ushort)obj);
return result;
case 9:
result = new NSNumber ((int)obj);
return result;
case 10:
result = new NSNumber ((uint)obj);
return result;
case 11:
result = new NSNumber ((long)obj);
return result;
case 12:
result = new NSNumber ((ulong)obj);
return result;
case 13:
result = new NSNumber ((float)obj);
return result;
case 14:
result = new NSNumber ((double)obj);
return result;
case 18:
result = new NSString ((string)obj);
return result;
}
if (type == typeof(IntPtr))
{
result = NSValue.ValueFromPointer ((IntPtr)obj);
}
else
{
if (type == typeof(SizeF))
{
result = NSValue.FromSizeF ((SizeF)obj);
}
else
{
if (type == typeof(RectangleF))
{
result = NSValue.FromRectangleF ((RectangleF)obj);
}
else
{
if (type == typeof(PointF))
{
result = NSValue.FromPointF ((PointF)obj);
}
else
{
INativeObject nativeObject = obj as INativeObject;
if (nativeObject != null)
{
result = Runtime.GetNSObject (nativeObject.Handle);
}
else
{
result = null;
}
}
}
}
}
}
}
return result;
}

Just like the FromObject conversion, you have to handle that case by case.
for NSNumbers, use explicit casting (works for bool as well):
NSNumber number;
double d = (double)number;
for NSStrings, implicit conversion even work:
NSString nsstring;
string converted = nsstring;
But I guess you already figured the value types conversions.
For reference type, all types deriving from NSObject (that's the case for most of the objects you want to pass back and forth to native) have a constructor overload taking an IntPtr as argument. use that.
NSObject native;
var label = new UILabel (native.Handle);
That being said, the NSObject is redundant. Your native library could just return an IntPtr (void*) you could use to recreate your objects. You DON'T have to do this:
IntPtr ptr;
NSObject nsobject = new NSObject (ptr);

Related

how to return the result outside the if statement

I was writing a revit pluging using c#. I couldn't return the result without assign public variable. Below is my code.I am not sure what is the error. Error in last codeline - return stid
public ElementId sysid(Duct elt, Document doc)
{
ElementId stid;
FilteredElementCollector DuctSystemcollector = new FilteredElementCollector(doc);
ICollection<Element> DuctSystem = DuctSystemcollector.OfClass(typeof(MechanicalSystemType)).ToElements();
foreach (Element j in DuctSystem)
{
string sn = j.GetParameters("Type Name")[0].AsString();
if (sn == elt.GetParameters("System Type")[0].AsValueString())
{
stid = j.Id;
//return stid;
//TaskDialog.Show("Duct shape ", stid);
}
}
return stid;
}

Convert AnyObject to NSString to Swift String

I am trying to understand why I cannot get a swift String from my dictionary type of AnyObject. If I do a println(fileCreationDate) it works, but I need an actual string of some kind to work with. So when I try to convert it to an NSString (because it is an object unlike String, a struct) it is nil.
Here is what I have:
if let atts = fileManager.attributesOfItemAtPath(fileLocation.path!, error:&err) as? Dictionary<String, AnyObject> {
println(atts)
if let fileCreationDate:AnyObject = atts["NSFileCreationDate"] {
println(fileCreationDate) //prints a date
var mystring:NSString! = fileCreationDate as? NSString
println(mystring) //prints nil
}
Thanks!
You have to use if let and a conditional cast to NSDate to convert your anyObject to NSDate and them you can format your date as you wish.
if let atts = NSFileManager.defaultManager().attributesOfItemAtPath(myUrl.path!, error:nil) as? Dictionary<String, AnyObject> {
if let fileCreationDate = atts["NSFileCreationDate"] as? NSDate {
println(fileCreationDate)
let mystring = fileCreationDate.description
println(mystring)
}
}
extension String {
var fileExists: Bool {
return NSFileManager.defaultManager().fileExistsAtPath(self)
}
var fileAttributes: [String:AnyObject] {
return fileExists ? NSFileManager.defaultManager().attributesOfItemAtPath(self, error:nil) as Dictionary<String, AnyObject> : [:]
}
var fileCreationDate:NSDate {
return fileAttributes["NSFileCreationDate"] as NSDate
}
var fileGroupOwnerAccountName:String{
return fileAttributes["NSFileGroupOwnerAccountName"] as String
}
var fileType: String {
return fileAttributes["NSFileType"] as String
}
var fileHFSTypeCode: Int {
return fileAttributes["NSFileHFSTypeCode"] as Int
}
var fileExtendedAttributes:[String:AnyObject] {
return fileAttributes["NSFileExtendedAttributes"] as [String:AnyObject]
}
var fileSystemNumber: Int {
return fileAttributes["NSFileSystemNumber"] as Int
}
var fileOwnerAccountName: String {
return fileAttributes["NSFileOwnerAccountName"] as String
}
var fileReferenceCount: Int {
return fileAttributes["NSFileReferenceCount"] as Int
}
var fileModificationDate: NSDate {
return fileAttributes["NSFileModificationDate"] as NSDate
}
var fileExtensionHidden: Bool {
return fileAttributes["NSFileExtensionHidden"] as Bool
}
var fileSize: Int {
return fileAttributes["NSFileSize"] as Int
}
var fileGroupOwnerAccountID: Int {
return fileAttributes["NSFileGroupOwnerAccountID"] as Int
}
var fileOwnerAccountID: Int {
return fileAttributes["NSFileOwnerAccountID"] as Int
}
var filePosixPermissions: Int {
return fileAttributes["NSFilePosixPermissions"] as Int
}
var fileHFSCreatorCode: Int {
return fileAttributes["NSFileHFSCreatorCode"] as Int
}
var fileSystemFileNumber: Int {
return fileAttributes["NSFileSystemFileNumber"] as Int
}
}

Google API throws exception at specified lat & long position?

Below is my sample code
public static string GetGeoLoc(string latitude, string longitude,
out string Address_ShortCountryName,
out string Address_country,
out string Address_administrative_area_level_1,
out string Address_administrative_area_level_1_short_name,
out string Address_administrative_area_level_2,
out string Address_administrative_area_level_3,
out string Address_colloquial_area,
out string Address_locality,
out string Address_sublocality,
out string Address_neighborhood)
{
Address_ShortCountryName = "";
Address_country = "";
Address_administrative_area_level_1 = "";
Address_administrative_area_level_1_short_name = "";
Address_administrative_area_level_2 = "";
Address_administrative_area_level_3 = "";
Address_colloquial_area = "";
Address_locality = "";
Address_sublocality = "";
Address_neighborhood = "";
XmlDocument doc = new XmlDocument();
try
{
doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + latitude + "," + longitude + "&sensor=false");
XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
if (element.InnerText == "ZERO_RESULTS")
{
return ("No data available for the specified location");
}
else
{
element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");
string longname = "";
string shortname = "";
string typename = "";
XmlNodeList xnList = doc.SelectNodes("//GeocodeResponse/result/address_component");
foreach (XmlNode xn in xnList)
{
try
{
longname = xn["long_name"].InnerText;
shortname = xn["short_name"].InnerText;
typename = xn["type"].InnerText;
switch (typename)
{
case "country":
{
Address_country = longname;
Address_ShortCountryName = shortname;
break;
}
case "locality":
{
Address_locality = longname;
break;
}
case "sublocality":
{
Address_sublocality = longname;
break;
}
case "neighborhood":
{
Address_neighborhood = longname;
break;
}
case "colloquial_area":
{
Address_colloquial_area = longname;
break;
}
case "administrative_area_level_1":
{
Address_administrative_area_level_1 = longname;
Address_administrative_area_level_1_short_name = shortname;
break;
}
case "administrative_area_level_2":
{
Address_administrative_area_level_2 = longname;
break;
}
case "administrative_area_level_3":
{
Address_administrative_area_level_3 = longname;
break;
}
default:
break;
}
}
catch (Exception e)
{
clsExHandler.Instance.Write(e);
}
}
return (element.InnerText);
}
}
catch (Exception ex)
{
return ("(Address lookup failed: ) " + ex.Message);
}
}
try passing latitude as 33.4965 & longitude as -112.205
i'm getting an exception object reference to invalid object in the line
**typename = xn["type"].InnerText;**
when i debug step by step there is no such attribute like ["type"]
Also there are some other lingual character why?
How could i resolve this issue.
I'm not familiar with c# and Im not sure if your code is correct at all(e.g. types is not an attribute, it's an elementNode).
Assuming that your code is correct and you can select nodes by using node['nameOfChildNode'] , when you inspect the XML-File: http://maps.googleapis.com/maps/api/geocode/xml?latlng=33.4965,-112.205&sensor=false you will see that there are address_components with 2 <type>'s and also address_components without any <type> .
I guess your code breaks not at the missing <type>, it breaks when you try to access a property(InnerText) of the missing <type>.
What you can do: use selectSingleNode to select the <type> and when it returns null implement a fallback or leave the further processing.
http://maps.googleapis.com/maps/api/geocode/json?latlng=33.4965%20,%20-112.205&sensor=false
returns
{
"results" : [],
"status" : "ZERO_RESULTS"
}
Therefore
XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
if (element.InnerText == "ZERO_RESULTS")
{
return ("No data available for the specified location");
}
is not catching ZERO_RESULTS.
I am not familiar with C# so I cannot help further.

JUnit Test Case for stmt if (obj == null) return false;

I am trying to write a JUnit Test Case for the below Java Code:
private java.lang.Object __equalsCalc = null;
public synchronized boolean equals(java.lang.Object obj) {
if (!(obj instanceof Crop))
return false;
Crop other = (Crop) obj;
if (obj == null) return false;
if (this == obj) return true;
if (__equalsCalc != null) {
return (__equalsCalc == obj);
}
}
The Test case i wote is below:
public void testCrop() {
Crop cp = new Crop();
Object obj = false;
cp.equals(obj);
assertSame(obj instanceof Crop, false);
}
public void testCrop1() {
Crop other = (Crop) obj;
// assertSame(other.equals(obj), true);
assertEquals(obj, other);
}
The test case for instanceof is working fine, but while checking if(obj == null) return false; the test case is failing:
You could use assertFalse
public void testEqualsNullIsFalse() {
Crop crop = new Crop();
assertFalse(crop.equals(null));
}
Test case has to be something like
Crop cp = new Crop();
Object obj = null;
bool expected = false;
assertEquals(expected,cp.equals(obj));
NB if you are trying do something like the as operator, have a look at this
how to emulate as operator in Java

Easy way to search a string for strings

I'm trying to find the easiest way to search a string for an array of possible strings. I know the easy way to do this for characters is to use myString.IndexOfAny(charArray). But how what if I'd like to search my string for strings and not just characters? Are there any .net tricks or methods that make this easier?
Basically, I'd like to do something like this:
string myName = "rahkim";
string[] names = new string[] {"joe","bob","chris"};
if(myName.IndexOfAny(names) >= 0)
{
//success code//
}
I know there are ways to do this with loops, etc. But I was hoping for something inherent in the framework.
You should define if you want to to find equal strings or search for a matching substring. Both ways are easy pre-LINQ and with LINQ.
string myName = "rahkim";
string[] names = new string[] { "joe", "bob", "chris" };
Equal Strings, LINQ
bool contains = names.Contains(myName);
Equal Strings, Pre-LINQ
bool contains = new List<string>(name).Contains(myName);
Substrings, LINQ
bool contains = names.Any(name => name.Contains(myName));
Substring, Pre-LINQ
bool contains = false;
foreach(string name in names)
if (name.Contains(myName))
contains = true;
If anyone else found this while trying to search for a .Net method like String.IndexOfAny(String[]), this is my solution:
C#
public int IndexOfAny(string test, string[] values)
{
int first = -1;
foreach (string item in values) {
int i = test.IndexOf(item);
if (i >= 0) {
if (first > 0) {
if (i < first) {
first = i;
}
} else {
first = i;
}
}
}
return first;
}
VB
Public Function IndexOfAny(test As String, values As String()) As Integer
Dim first As Integer = -1
For Each item As String In values
Dim i As Integer = test.IndexOf(item)
If i >= 0 Then
If first > 0 Then
If i < first Then
first = i
End If
Else
first = i
End If
End If
Next
Return first
End Function
You can do a LastIndexOfAny(String[]) by just switching the
i < first
to
i > first
You can (also) use the static IndexOf method of the Array class:
bool hasName = Array.IndexOf(names, myName) > -1;
int IndexOfAny(String[] rgs) would indeed be nice but it's nominally an O(n^2) operation. If, in your application, the set of strings rgs is large and always the same, the most efficient approach is to load them into a trie data structure once, and then use the trie repeatedly to search for them within the unknown strings given at runtime.
Here is the relevant code, adapted from a C# trie source I found on the web, attributed to "Kerry D. Wong." In my version, each string in the trie has a "payload" of generic type TValue. To use this trie to simply search for substrings, the payload could always be set to true, as illustrated with simple_trie.
The other thing I changed here is that this trie automatically adapts allow for storage of arbitrary Unicode strings. The array at each node—which characterizes a trie—adjusts its base and length to accomodate the range of Unicode characters which need to be stored at that node. This allows for case-sensitive matching, for example.
The C# 3.0 initialization syntax is handy for this trie, but enabling it requires a dummy implementation of IEnumerable in order to compile. The CLR doesn't seem to call GetEnumerator() and I suggest that you don't try to enumerate with its result either.
using System;
using System.Collections.Generic;
using System.Linq; // only used in Main()
class Program
{
// trie with payload of type <String>
static Trie<String> value_trie = new Trie<String>
{
{ "rabbit", "cute" },
{ "giraffe", "tall" },
{ "ape", "smart" },
{ "hippo", "large" },
};
// degenerate case of a trie without payload
static Trie<bool> simple_trie = new Trie<bool>
{
{ "rabbit", true },
{ "giraffe", true },
{ "ape", true },
{ "hippo", true },
};
static void Main(String[] args)
{
String s = "Once upon a time, a rabbit met an ape in the woods.";
// Retrieve payloads for words in the string.
//
// output:
// cute
// smart
foreach (String word in value_trie.AllSubstringValues(s))
Console.WriteLine(word);
// Simply test a string for any of the words in the trie.
// Note that the Any() operator ensures that the input is no longer
// traversed once a single result is found.
//
// output:
// True
Console.WriteLine(simple_trie.AllSubstringValues(s).Any(e=>e));
s = "Four score and seven years ago.";
// output:
// False
Console.WriteLine(simple_trie.AllSubstringValues(s).Any(e => e));
}
}
class TrieNode<TValue>
{
private TrieNode<TValue>[] nodes = null;
private TValue m_value = default(TValue);
private Char m_base;
public Char Base { get { return m_base; } }
public bool IsEnd { get { return !m_value.Equals(default(TValue)); } }
public TValue Value
{
get { return m_value; }
set { m_value = value; }
}
public IEnumerable<TrieNode<TValue>> Nodes { get { return nodes; } }
public TrieNode<TValue> this[char c]
{
get
{
if (nodes != null && m_base <= c && c < m_base + nodes.Length)
return nodes[c - m_base];
return null;
}
}
public TrieNode<TValue> AddChild(char c)
{
if (nodes == null)
{
m_base = c;
nodes = new TrieNode<TValue>[1];
}
else if (c >= m_base + nodes.Length)
{
Array.Resize(ref nodes, c - m_base + 1);
}
else if (c < m_base)
{
Char c_new = (Char)(m_base - c);
TrieNode<TValue>[] tmp = new TrieNode<TValue>[nodes.Length + c_new];
nodes.CopyTo(tmp, c_new);
m_base = c;
nodes = tmp;
}
TrieNode<TValue> node = nodes[c - m_base];
if (node == null)
{
node = new TrieNode<TValue>();
nodes[c - m_base] = node;
}
return node;
}
};
class Trie<TValue> : System.Collections.IEnumerable
{
private TrieNode<TValue> _root = new TrieNode<TValue>();
// This dummy enables C# 3.0 initialization syntax
public System.Collections.IEnumerator GetEnumerator()
{
return null;
}
public void Add(String s, TValue v)
{
TrieNode<TValue> node = _root;
foreach (Char c in s)
node = node.AddChild(c);
node.Value = v;
}
public bool Contains(String s)
{
TrieNode<TValue> node = _root;
foreach (Char c in s)
{
node = node[c];
if (node == null)
return false;
}
return node.IsEnd;
}
public TValue Find(String s_in)
{
TrieNode<TValue> node = _root;
foreach (Char c in s_in)
{
node = node[c];
if (node == null)
return default(TValue);
}
return node.Value;
}
public IEnumerable<TValue> FindAll(String s_in)
{
TrieNode<TValue> node = _root;
foreach (Char c in s_in)
{
node = node[c];
if (node == null)
break;
if (node.Value != null)
yield return node.Value;
}
}
public IEnumerable<TValue> AllSubstringValues(String s)
{
int i_cur = 0;
while (i_cur < s.Length)
{
TrieNode<TValue> node = _root;
int i = i_cur;
while (i < s.Length)
{
node = node[s[i]];
if (node == null)
break;
if (node.Value != null)
yield return node.Value;
i++;
}
i_cur++;
}
}
};
Here's the right syntax:
if(names.Contains(myName))
{
//success code//
}
if (names.Contains(myName))
{
//success code//
}

Resources