Skip to content

Instantly share code, notes, and snippets.

@mloenow
Created December 1, 2011 12:50
Show Gist options
  • Select an option

  • Save mloenow/1416503 to your computer and use it in GitHub Desktop.

Select an option

Save mloenow/1416503 to your computer and use it in GitHub Desktop.
NSDictionary and NSArray dumping with extension methods
using System.Collections.Generic;
using System.Diagnostics;
using MonoTouch.Foundation;
namespace MonoTouch.Utils {
public static class Extensions {
public static string Dump(this NSDictionary dictionary, int level = 0) {
var dump = "";
if (dictionary != null) {
foreach (var keyvaluepair in dictionary)
dump += string.Format("{0}{1}: {2}\n", Tabs(level), keyvaluepair.Key.Stringyfy(), keyvaluepair.Value is NSDictionary || keyvaluepair.Value is NSMutableDictionary ? "\n" + (keyvaluepair.Value as NSDictionary).Dump(level += 1) : keyvaluepair.Value is NSArray ? (keyvaluepair.Value as NSArray).Dump(level) : keyvaluepair.Value.ToString());
}
if (string.IsNullOrEmpty(dump)) Debug.WriteLine("EMPTY");
return dump;
}
public static string Dump(this NSArray array, int level = 0) {
var arr = new List<string>();
for (uint i = 0; i < array.Count; i++) {
var o = MonoTouch.ObjCRuntime.Runtime.GetNSObject(array.ValueAt(i));
arr.Add(o is NSArray ? (o as NSArray).Dump() : o is NSDictionary || o is NSMutableDictionary ? "\n" + Tabs(level) + (o as NSDictionary).Dump(level) : o.ToString());
}
return string.Join(", ", arr.ToArray());
}
static string Tabs(int count) {
var tabs = "";
for (int i = 0; i < count; i++)
tabs += "\t";
return tabs;
}
public static string Stringyfy(this NSObject o) {
return o.ToString().Stringyfy();
}
public static string Stringyfy(this string o) {
return string.Format("\"{0}\"", o);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment