{"id":562,"date":"2014-04-10T14:00:00","date_gmt":"2014-04-10T14:00:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=562"},"modified":"2025-02-25T13:14:15","modified_gmt":"2025-02-25T13:14:15","slug":"nspointerarray","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/nspointerarray","title":{"rendered":"NSPointerArray and Collections of Weak Objects (Objective &#8211; C)"},"content":{"rendered":"\n<p>There will be times when you want a collection of weak references. \u00a0Any one to many notification chain is just begging to introduce reference cycles more than willing to leak all over your application. \u00a0So when you have this situation in your application, you might want to consider the <a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/cocoa\/reference\/foundation\/classes\/NSPointerArray_Class\/Introduction\/Introduction.html\" rel=\"noopener\">NSPointerArray<\/a>. \u00a0Attached is an example of the NSPointerArray in action.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"code-sample\">Code Sample<\/h2>\n\n\n\n<p>The NSPointerArray will allow you to either have a collection of strong or weak pointers and with nice little static call we can start using this collection.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">NSPointerArray* myArray;\nmyArray = [NSPointerArray weakObjectsPointerArray];<\/pre>\n\n\n\n<p>Now rather than adding objects to the collection we explicitly add the pointers to the collection.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">obj1 = [[UIView alloc]init];\nobj1.tag = 100;\n[myArray addPointer:(__bridge void *)obj1];<\/pre>\n\n\n\n<p>And if you want to get a value out of it, just use the pointerAtIndex: method to get the object out. The only caveat here is that since the collection is weak, than the pointer coming out may be a nil value. &nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[myArray pointerAtIndex:someIndex];<\/pre>\n\n\n\n<p>As with many things related to APIs, seeing it in action is better than reading about it. &nbsp;So I am attaching a sample showing how this works.<\/p>\n\n\n\n<p><a href=\"\/community\/cfs-file.ashx\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/baldnbearded.NSPointerArray\/5123.NSPointerArraySample.zip\">NSPointerArray Example Code<\/a><\/p>\n\n\n\n<p>If you have a <a href=\"http:\/\/ideas.infragistics.com\/forums\/211525-nuclios\">request for a feature in our frameworks<\/a>, please let us know and we will see what can be done.&nbsp;<\/p>\n\n\n\n<p>Questions about a feature? &nbsp;Hit us on the <a href=\"\/community\/forums\/default.aspx?GroupID=92\">NucliOS Forums<\/a>.<\/p>\n\n\n\n<p>And if you have a request for a how to article for the NucliOS product you can tweet it to the @<a href=\"https:\/\/twitter.com\/infragistics\/\" rel=\"noopener\">Infragistics<\/a> feed or myself <a href=\"https:\/\/twitter.com\/darrellkress17\" rel=\"noopener\">@DarrellKress17<\/a>&nbsp;and we will see if we can write that up for you.<\/p>\n\n\n\n<p><a href=\"http:\/\/plus.google.com\/111641280922250694088?rel=author\" rel=\"noopener\">By Darrell Kress<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"\/products\/ios\/\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"\/community\/cfs-file.ashx\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/baldnbearded\/4718.NucliOS_2D00_728x90b.jpg\" alt=\"\"\/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>There will be times when you want a collection of weak references. \u00a0Any one to many notification chain is just begging to introduce reference cycles more than willing to leak all over your application. \u00a0So when you have this situation in your application, you might want to consider the NSPointerArray. \u00a0Attached is an example of [&hellip;]<\/p>\n","protected":false},"author":56,"featured_media":2366,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-562","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=562"}],"version-history":[{"count":2,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/562\/revisions"}],"predecessor-version":[{"id":2113,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/562\/revisions\/2113"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/2366"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}