{"id":20826,"date":"2018-03-07T15:38:52","date_gmt":"2018-03-07T20:38:52","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=20826"},"modified":"2018-03-12T15:28:37","modified_gmt":"2018-03-12T19:28:37","slug":"getting-the-current-nsbundle","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/03\/07\/getting-the-current-nsbundle\/","title":{"rendered":"Getting the Current NSBundle"},"content":{"rendered":"<p><a href=\"http:\/\/bou.io\/NSBundle.current.html\">Nicolas Bouilleaud<\/a>:<\/p>\n<blockquote cite=\"http:\/\/bou.io\/NSBundle.current.html\"><ol><li>Use <code>backtrace()<\/code> to find the function pointer of the caller;<\/li><li>Use <code>dladdr()<\/code> to find the executable <em>image path<\/em> containing this function;<\/li><li>Find the loaded bundle with this executable path.<\/li><\/ol><p>[&#8230;]<\/p><p>With this, I can finally write <code>Bundle.current<\/code> everywhere, instead of <code>Bundle.main<\/code> or <code>Bundle(for: AnyClass)<\/code>.<\/p><\/blockquote>\n\n<p>Update (2018-03-12): <a href=\"https:\/\/twitter.com\/LeoNatan\/status\/972849717641207808\">Leo Natan<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/LeoNatan\/status\/972849717641207808\">\n<p>Very cool, but this is an error prone approach due to inlining and optimizations.<\/p>\n<p>For performance, <code>__builtin_frame_address()<\/code>\/<code>__builtin_return_address()<\/code> either directly in Swift if possible or in a C shim function would work quite well. Same risks as original approach.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Nicolas Bouilleaud: Use backtrace() to find the function pointer of the caller;Use dladdr() to find the executable image path containing this function;Find the loaded bundle with this executable path.[&#8230;]With this, I can finally write Bundle.current everywhere, instead of Bundle.main or Bundle(for: AnyClass). Update (2018-03-12): Leo Natan: Very cool, but this is an error prone approach [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[],"tags":[69,205,31,1472,30,1529,71,901],"class_list":["post-20826","post","type-post","status-publish","format-standard","hentry","tag-cocoa","tag-dyld","tag-ios","tag-ios-11","tag-mac","tag-macos-10-13","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=20826"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20826\/revisions"}],"predecessor-version":[{"id":20856,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20826\/revisions\/20856"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=20826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=20826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=20826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}