{"id":3666,"date":"2013-03-22T17:20:38","date_gmt":"2013-03-23T00:20:38","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=3666"},"modified":"2019-07-03T10:12:30","modified_gmt":"2019-07-03T17:12:30","slug":"producing-better-bindings-2-fields","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/producing-better-bindings-2-fields\/","title":{"rendered":"Producing Better Bindings #2: Fields"},"content":{"rendered":"<p>\t\t\t\tThis blog post is about producing better bindings of Objective-C libraries for <a href=\"http:\/\/xamarin.com\/monotouch\">Xamarin.iOS<\/a> and <a href=\"http:\/\/xamarin.com\/mac\">Xamarin.Mac<\/a>. Read the series <a href=\"\/producing-better-bindings-for-xamarin.ios-and-xamarin.mac\/\">introduction<\/a> to get a better idea why this is important and can save you time and headaches.<\/p>\n<p><strong>What can go wrong ?<\/strong><\/p>\n<p>Maybe nothing! Not every library use fields, e.g. there are no fields in the <a href=\"https:\/\/github.com\/mono\/monotouch-bindings\/tree\/master\/cocos2d\">Cocos2D library<\/a>. However, you should keep this <a href=\"https:\/\/github.com\/mono\/monotouch-bindings\/blob\/master\/cocos2d\/binding-test\/BindingFieldTest.cs\">fixture<\/a> in your binding test suite. If any fields are added in the future, then they&#8217;ll be tested without further work.<\/p>\n<p>For bindings that use fields, the most common issue will be typos. The compiler cannot validate the strings, so typos are sadly common, e.g. an extra space <code>[Field (\"MyField \")]<\/code>.<\/p>\n<p>You&#8217;ll also find that some fields might not really exist anymore in a newer version of the bound library, so it&#8217;s a handy check to have when updating a native library in your existing bindings.<\/p>\n<p><strong>What can we check for ?<\/strong><\/p>\n<p>When an\u00a0<code>NSString<\/code> is defined, it&#8217;s generally because it has a non-null value. OTOH if any error (e.g. a typo) is made in the attribute, then you&#8217;re likely to get a <code>null<\/code> result. This makes a great and easy condition to test.<\/p>\n<p><strong>Why is this important ?<\/strong><\/p>\n<p>You might be giving <code>null<\/code> values to some APIs that will either not work as you expect or will crash. Of course you won&#8217;t notice it before you actually use that specific field. This is not something that is easy to debug without looking at the binding source code \u2014something most consumers would prefer to avoid.<\/p>\n<p><strong>How to fix issues ?<\/strong><\/p>\n<p>In most cases, it&#8217;s simply fixing the typo inside the <code>[Field(\"\")]<\/code> attribute based on the library&#8217;s documentation and\/or header files.<\/p>\n<p>You might find some cases where a field is really meant to be <code>null<\/code>. If this happens, you may skip the test for an entire type or a single property by overriding either <code>Skip(Type)<\/code> or <code>Skip(PropertyInfo)<\/code> methods.<\/p>\n<pre class=\"lang:csharp decode:true\">\n\t[TestFixture]\n\tpublic class BindingFieldTest : ApiFieldTest {\n\n\t\tprotected override bool Skip (Type type)\n\t\t{\n\t\t\t\/\/ uncommon - but you could skip an entire type from being checked\n\t\t\tif (type.FullName == &quot;BrokenType&quot;)\n\t\t\t\treturn true;\n\t\t\treturn base.Skip (type);\n\t\t}\n\n\t\tprotected override bool Skip (PropertyInfo p)\n\t\t{\n\t\t\t\/\/ if some fields are meant to be null you can exclude them here\n\t\t\tswitch (p.Name) {\n\t\t\tcase &quot;FieldKnownToReturnNull&quot;:\n\t\t\t\treturn true;\n\t\t\tdefault:\n\t\t\t\treturn base.Skip (p);\n\t\t\t}\n\t\t}\n\t}\n<\/pre>\n<p><strong>What&#8217;s missing ?<\/strong><\/p>\n<p>The current implementation only validates fields that return\u00a0<code>NSString<\/code>. With other types, such as integers and floats, it&#8217;s not easy to distinguish between valid and invalid based on their value. The information required to query the symbol (e.g. <code>dlsym<\/code>) is not available, using reflection, in the assembly (i.e. the attribute is used to generate code).<\/p>\n<p>Also, and just like constructors, we cannot detect missing fields. You might want to grep header files (.h) and your binding files to see if number matches.<\/p>\n<p><b>Read the rest of the series:<\/b><\/p>\n<ul>\n<li><a href=\"\/producing-better-bindings-for-xamarin.ios-and-xamarin.mac\/\">Introduction<\/a><\/li>\n<li><a href=\"\/producing-better-bindings-1-constructors\/\">Part 1: Constructors<\/a><\/li>\n<li><a href=\"\/producing-better-bindings-2-fields\/\">Part 2: Fields<\/a><\/li>\n<li><a href=\"\/producing-better-bindings-3-selectors\/\">Part 3: Selectors<\/a><\/li>\n<li><a href=\"\/producing-better-bindings-4-signatures\/\">Part 4: Signatures<\/a><\/li>\n<\/ul>\n<p><a href=\"http:\/\/forums.xamarin.com\/discussion\/2433\/producing-better-bindings\">Discuss this post on the Xamarin forums<\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post is about producing better bindings of Objective-C libraries for Xamarin.iOS and Xamarin.Mac. Read the series introduction to get a better idea why this is important and can save you time and headaches. What can go wrong ? Maybe nothing! Not every library use fields, e.g. there are no fields in the Cocos2D [&hellip;]<\/p>\n","protected":false},"author":5741,"featured_media":39167,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[303],"tags":[4],"class_list":["post-3666","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>This blog post is about producing better bindings of Objective-C libraries for Xamarin.iOS and Xamarin.Mac. Read the series introduction to get a better idea why this is important and can save you time and headaches. What can go wrong ? Maybe nothing! Not every library use fields, e.g. there are no fields in the Cocos2D [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/3666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/5741"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=3666"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/3666\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=3666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=3666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=3666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}