swift equivalent of [NSBundle bundleForClass:[self class]]
What is swift equivalent of next code:
[NSBundle bundleForClass:[self class]]
I need load resources from test bundle (JSON data)
swift
add a comment |
What is swift equivalent of next code:
[NSBundle bundleForClass:[self class]]
I need load resources from test bundle (JSON data)
swift
add a comment |
What is swift equivalent of next code:
[NSBundle bundleForClass:[self class]]
I need load resources from test bundle (JSON data)
swift
What is swift equivalent of next code:
[NSBundle bundleForClass:[self class]]
I need load resources from test bundle (JSON data)
swift
swift
asked Sep 3 '14 at 18:39
DanilDanil
1,19921126
1,19921126
add a comment |
add a comment |
8 Answers
8
active
oldest
votes
Never used, but I think it should be this:
Swift <= 2.x
NSBundle(forClass: self.dynamicType)
Swift 3.x
Bundle(for: type(of: self))
37
Autocomplete in the current version of Xcode acts like it has no idea what you're talking about, but it does indeed work.
– David Beck
Oct 7 '14 at 21:52
2
Yanno. I just love this place! Thanks!
– Little Green Viper
Jun 28 '15 at 1:55
3
in version 2.1: NSBundle(forClass: self)
– zwebie
Apr 15 '16 at 10:04
The evolution thread to consider changing this is at github.com/apple/swift-evolution/blob/master/proposals/…
– William Entriken
Apr 25 '16 at 22:07
1
The solution doesn't work for value types. Consider usinginit?(identifier: String)
where identifier is your target's bundle ID. Another solution for value types is to declare an empty class inside your value type. Example of the latter solution:Bundle(for: Foo.Bar.self)
whereFoo
- your struct,Bar
- some inner class.
– Vadim Bulavin
Apr 24 '17 at 13:37
|
show 1 more comment
Swift 3:
Bundle(for: type(of: self))
3
The solution doesn't work for value types. Consider usinginit?(identifier: String)
where identifier is your target's bundle ID. If you don't want to hardcode the bundle ID, useBundle(for: Foo.Bar.self)
whereFoo
- your struct,Bar
- some inner class.
– Vadim Bulavin
Apr 24 '17 at 13:43
add a comment |
I personally like:
let bun = NSBundle(forClass: self.classForCoder)
1
This works best for Swift 3.1 (Xcode 8.3)
– adib
Aug 13 '17 at 6:48
add a comment |
let bundle = NSBundle(forClass:object_getClass(self))
add a comment |
The selected answer did not work for me in a static method of a UIView subclass, but I found this:
Bundle(for: self.classForCoder)
This also works when you want to get the Bundle
within a test target.
This saved me! Thanks!
– devjme
Feb 24 at 1:03
add a comment |
Loading the xib for dynamicType of the class
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "CellForAlert", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil).first as! UIView
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.addSubview(view);
add a comment |
In Swift 3.0, you can use:
func kZWGetBundle() -> Bundle{
return Bundle(for: AnyClass.self as! AnyClass)
}
1
It just crashes
– Alexander Belyavskiy
Dec 13 '16 at 16:22
Crashes for me too, down voted for using force unwrap in sample code.
– SafeFastExpressive
Sep 29 '18 at 22:43
add a comment |
If you are working on a class then
Bundle(for: type(of: self))
Sometimes you may work in a struct, then you need to use any class in the bundle
Bundle(for: AnyClassInTheBundle.self)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25651403%2fswift-equivalent-of-nsbundle-bundleforclassself-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
Never used, but I think it should be this:
Swift <= 2.x
NSBundle(forClass: self.dynamicType)
Swift 3.x
Bundle(for: type(of: self))
37
Autocomplete in the current version of Xcode acts like it has no idea what you're talking about, but it does indeed work.
– David Beck
Oct 7 '14 at 21:52
2
Yanno. I just love this place! Thanks!
– Little Green Viper
Jun 28 '15 at 1:55
3
in version 2.1: NSBundle(forClass: self)
– zwebie
Apr 15 '16 at 10:04
The evolution thread to consider changing this is at github.com/apple/swift-evolution/blob/master/proposals/…
– William Entriken
Apr 25 '16 at 22:07
1
The solution doesn't work for value types. Consider usinginit?(identifier: String)
where identifier is your target's bundle ID. Another solution for value types is to declare an empty class inside your value type. Example of the latter solution:Bundle(for: Foo.Bar.self)
whereFoo
- your struct,Bar
- some inner class.
– Vadim Bulavin
Apr 24 '17 at 13:37
|
show 1 more comment
Never used, but I think it should be this:
Swift <= 2.x
NSBundle(forClass: self.dynamicType)
Swift 3.x
Bundle(for: type(of: self))
37
Autocomplete in the current version of Xcode acts like it has no idea what you're talking about, but it does indeed work.
– David Beck
Oct 7 '14 at 21:52
2
Yanno. I just love this place! Thanks!
– Little Green Viper
Jun 28 '15 at 1:55
3
in version 2.1: NSBundle(forClass: self)
– zwebie
Apr 15 '16 at 10:04
The evolution thread to consider changing this is at github.com/apple/swift-evolution/blob/master/proposals/…
– William Entriken
Apr 25 '16 at 22:07
1
The solution doesn't work for value types. Consider usinginit?(identifier: String)
where identifier is your target's bundle ID. Another solution for value types is to declare an empty class inside your value type. Example of the latter solution:Bundle(for: Foo.Bar.self)
whereFoo
- your struct,Bar
- some inner class.
– Vadim Bulavin
Apr 24 '17 at 13:37
|
show 1 more comment
Never used, but I think it should be this:
Swift <= 2.x
NSBundle(forClass: self.dynamicType)
Swift 3.x
Bundle(for: type(of: self))
Never used, but I think it should be this:
Swift <= 2.x
NSBundle(forClass: self.dynamicType)
Swift 3.x
Bundle(for: type(of: self))
edited Dec 26 '16 at 23:17
answered Sep 3 '14 at 18:42
AntonioAntonio
58.6k9126148
58.6k9126148
37
Autocomplete in the current version of Xcode acts like it has no idea what you're talking about, but it does indeed work.
– David Beck
Oct 7 '14 at 21:52
2
Yanno. I just love this place! Thanks!
– Little Green Viper
Jun 28 '15 at 1:55
3
in version 2.1: NSBundle(forClass: self)
– zwebie
Apr 15 '16 at 10:04
The evolution thread to consider changing this is at github.com/apple/swift-evolution/blob/master/proposals/…
– William Entriken
Apr 25 '16 at 22:07
1
The solution doesn't work for value types. Consider usinginit?(identifier: String)
where identifier is your target's bundle ID. Another solution for value types is to declare an empty class inside your value type. Example of the latter solution:Bundle(for: Foo.Bar.self)
whereFoo
- your struct,Bar
- some inner class.
– Vadim Bulavin
Apr 24 '17 at 13:37
|
show 1 more comment
37
Autocomplete in the current version of Xcode acts like it has no idea what you're talking about, but it does indeed work.
– David Beck
Oct 7 '14 at 21:52
2
Yanno. I just love this place! Thanks!
– Little Green Viper
Jun 28 '15 at 1:55
3
in version 2.1: NSBundle(forClass: self)
– zwebie
Apr 15 '16 at 10:04
The evolution thread to consider changing this is at github.com/apple/swift-evolution/blob/master/proposals/…
– William Entriken
Apr 25 '16 at 22:07
1
The solution doesn't work for value types. Consider usinginit?(identifier: String)
where identifier is your target's bundle ID. Another solution for value types is to declare an empty class inside your value type. Example of the latter solution:Bundle(for: Foo.Bar.self)
whereFoo
- your struct,Bar
- some inner class.
– Vadim Bulavin
Apr 24 '17 at 13:37
37
37
Autocomplete in the current version of Xcode acts like it has no idea what you're talking about, but it does indeed work.
– David Beck
Oct 7 '14 at 21:52
Autocomplete in the current version of Xcode acts like it has no idea what you're talking about, but it does indeed work.
– David Beck
Oct 7 '14 at 21:52
2
2
Yanno. I just love this place! Thanks!
– Little Green Viper
Jun 28 '15 at 1:55
Yanno. I just love this place! Thanks!
– Little Green Viper
Jun 28 '15 at 1:55
3
3
in version 2.1: NSBundle(forClass: self)
– zwebie
Apr 15 '16 at 10:04
in version 2.1: NSBundle(forClass: self)
– zwebie
Apr 15 '16 at 10:04
The evolution thread to consider changing this is at github.com/apple/swift-evolution/blob/master/proposals/…
– William Entriken
Apr 25 '16 at 22:07
The evolution thread to consider changing this is at github.com/apple/swift-evolution/blob/master/proposals/…
– William Entriken
Apr 25 '16 at 22:07
1
1
The solution doesn't work for value types. Consider using
init?(identifier: String)
where identifier is your target's bundle ID. Another solution for value types is to declare an empty class inside your value type. Example of the latter solution: Bundle(for: Foo.Bar.self)
where Foo
- your struct, Bar
- some inner class.– Vadim Bulavin
Apr 24 '17 at 13:37
The solution doesn't work for value types. Consider using
init?(identifier: String)
where identifier is your target's bundle ID. Another solution for value types is to declare an empty class inside your value type. Example of the latter solution: Bundle(for: Foo.Bar.self)
where Foo
- your struct, Bar
- some inner class.– Vadim Bulavin
Apr 24 '17 at 13:37
|
show 1 more comment
Swift 3:
Bundle(for: type(of: self))
3
The solution doesn't work for value types. Consider usinginit?(identifier: String)
where identifier is your target's bundle ID. If you don't want to hardcode the bundle ID, useBundle(for: Foo.Bar.self)
whereFoo
- your struct,Bar
- some inner class.
– Vadim Bulavin
Apr 24 '17 at 13:43
add a comment |
Swift 3:
Bundle(for: type(of: self))
3
The solution doesn't work for value types. Consider usinginit?(identifier: String)
where identifier is your target's bundle ID. If you don't want to hardcode the bundle ID, useBundle(for: Foo.Bar.self)
whereFoo
- your struct,Bar
- some inner class.
– Vadim Bulavin
Apr 24 '17 at 13:43
add a comment |
Swift 3:
Bundle(for: type(of: self))
Swift 3:
Bundle(for: type(of: self))
answered Sep 22 '16 at 9:07
Alexander BelyavskiyAlexander Belyavskiy
1,0721620
1,0721620
3
The solution doesn't work for value types. Consider usinginit?(identifier: String)
where identifier is your target's bundle ID. If you don't want to hardcode the bundle ID, useBundle(for: Foo.Bar.self)
whereFoo
- your struct,Bar
- some inner class.
– Vadim Bulavin
Apr 24 '17 at 13:43
add a comment |
3
The solution doesn't work for value types. Consider usinginit?(identifier: String)
where identifier is your target's bundle ID. If you don't want to hardcode the bundle ID, useBundle(for: Foo.Bar.self)
whereFoo
- your struct,Bar
- some inner class.
– Vadim Bulavin
Apr 24 '17 at 13:43
3
3
The solution doesn't work for value types. Consider using
init?(identifier: String)
where identifier is your target's bundle ID. If you don't want to hardcode the bundle ID, use Bundle(for: Foo.Bar.self)
where Foo
- your struct, Bar
- some inner class.– Vadim Bulavin
Apr 24 '17 at 13:43
The solution doesn't work for value types. Consider using
init?(identifier: String)
where identifier is your target's bundle ID. If you don't want to hardcode the bundle ID, use Bundle(for: Foo.Bar.self)
where Foo
- your struct, Bar
- some inner class.– Vadim Bulavin
Apr 24 '17 at 13:43
add a comment |
I personally like:
let bun = NSBundle(forClass: self.classForCoder)
1
This works best for Swift 3.1 (Xcode 8.3)
– adib
Aug 13 '17 at 6:48
add a comment |
I personally like:
let bun = NSBundle(forClass: self.classForCoder)
1
This works best for Swift 3.1 (Xcode 8.3)
– adib
Aug 13 '17 at 6:48
add a comment |
I personally like:
let bun = NSBundle(forClass: self.classForCoder)
I personally like:
let bun = NSBundle(forClass: self.classForCoder)
answered Jan 27 '16 at 22:11
SakiboySakiboy
3,93513451
3,93513451
1
This works best for Swift 3.1 (Xcode 8.3)
– adib
Aug 13 '17 at 6:48
add a comment |
1
This works best for Swift 3.1 (Xcode 8.3)
– adib
Aug 13 '17 at 6:48
1
1
This works best for Swift 3.1 (Xcode 8.3)
– adib
Aug 13 '17 at 6:48
This works best for Swift 3.1 (Xcode 8.3)
– adib
Aug 13 '17 at 6:48
add a comment |
let bundle = NSBundle(forClass:object_getClass(self))
add a comment |
let bundle = NSBundle(forClass:object_getClass(self))
add a comment |
let bundle = NSBundle(forClass:object_getClass(self))
let bundle = NSBundle(forClass:object_getClass(self))
edited Oct 27 '14 at 18:32
answered Oct 27 '14 at 18:26
Mark SemselMark Semsel
5,94432225
5,94432225
add a comment |
add a comment |
The selected answer did not work for me in a static method of a UIView subclass, but I found this:
Bundle(for: self.classForCoder)
This also works when you want to get the Bundle
within a test target.
This saved me! Thanks!
– devjme
Feb 24 at 1:03
add a comment |
The selected answer did not work for me in a static method of a UIView subclass, but I found this:
Bundle(for: self.classForCoder)
This also works when you want to get the Bundle
within a test target.
This saved me! Thanks!
– devjme
Feb 24 at 1:03
add a comment |
The selected answer did not work for me in a static method of a UIView subclass, but I found this:
Bundle(for: self.classForCoder)
This also works when you want to get the Bundle
within a test target.
The selected answer did not work for me in a static method of a UIView subclass, but I found this:
Bundle(for: self.classForCoder)
This also works when you want to get the Bundle
within a test target.
edited Dec 8 '17 at 0:17
Community♦
11
11
answered May 30 '17 at 17:50
SiegfoultSiegfoult
1,3831318
1,3831318
This saved me! Thanks!
– devjme
Feb 24 at 1:03
add a comment |
This saved me! Thanks!
– devjme
Feb 24 at 1:03
This saved me! Thanks!
– devjme
Feb 24 at 1:03
This saved me! Thanks!
– devjme
Feb 24 at 1:03
add a comment |
Loading the xib for dynamicType of the class
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "CellForAlert", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil).first as! UIView
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.addSubview(view);
add a comment |
Loading the xib for dynamicType of the class
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "CellForAlert", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil).first as! UIView
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.addSubview(view);
add a comment |
Loading the xib for dynamicType of the class
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "CellForAlert", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil).first as! UIView
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.addSubview(view);
Loading the xib for dynamicType of the class
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "CellForAlert", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil).first as! UIView
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.addSubview(view);
edited Apr 20 '16 at 10:18
answered Apr 20 '16 at 10:10
Ayaz AkbarAyaz Akbar
1,07011022
1,07011022
add a comment |
add a comment |
In Swift 3.0, you can use:
func kZWGetBundle() -> Bundle{
return Bundle(for: AnyClass.self as! AnyClass)
}
1
It just crashes
– Alexander Belyavskiy
Dec 13 '16 at 16:22
Crashes for me too, down voted for using force unwrap in sample code.
– SafeFastExpressive
Sep 29 '18 at 22:43
add a comment |
In Swift 3.0, you can use:
func kZWGetBundle() -> Bundle{
return Bundle(for: AnyClass.self as! AnyClass)
}
1
It just crashes
– Alexander Belyavskiy
Dec 13 '16 at 16:22
Crashes for me too, down voted for using force unwrap in sample code.
– SafeFastExpressive
Sep 29 '18 at 22:43
add a comment |
In Swift 3.0, you can use:
func kZWGetBundle() -> Bundle{
return Bundle(for: AnyClass.self as! AnyClass)
}
In Swift 3.0, you can use:
func kZWGetBundle() -> Bundle{
return Bundle(for: AnyClass.self as! AnyClass)
}
edited Dec 12 '16 at 14:53
jrbedard
2,92852131
2,92852131
answered Dec 12 '16 at 14:33
InitialCInitialC
193
193
1
It just crashes
– Alexander Belyavskiy
Dec 13 '16 at 16:22
Crashes for me too, down voted for using force unwrap in sample code.
– SafeFastExpressive
Sep 29 '18 at 22:43
add a comment |
1
It just crashes
– Alexander Belyavskiy
Dec 13 '16 at 16:22
Crashes for me too, down voted for using force unwrap in sample code.
– SafeFastExpressive
Sep 29 '18 at 22:43
1
1
It just crashes
– Alexander Belyavskiy
Dec 13 '16 at 16:22
It just crashes
– Alexander Belyavskiy
Dec 13 '16 at 16:22
Crashes for me too, down voted for using force unwrap in sample code.
– SafeFastExpressive
Sep 29 '18 at 22:43
Crashes for me too, down voted for using force unwrap in sample code.
– SafeFastExpressive
Sep 29 '18 at 22:43
add a comment |
If you are working on a class then
Bundle(for: type(of: self))
Sometimes you may work in a struct, then you need to use any class in the bundle
Bundle(for: AnyClassInTheBundle.self)
add a comment |
If you are working on a class then
Bundle(for: type(of: self))
Sometimes you may work in a struct, then you need to use any class in the bundle
Bundle(for: AnyClassInTheBundle.self)
add a comment |
If you are working on a class then
Bundle(for: type(of: self))
Sometimes you may work in a struct, then you need to use any class in the bundle
Bundle(for: AnyClassInTheBundle.self)
If you are working on a class then
Bundle(for: type(of: self))
Sometimes you may work in a struct, then you need to use any class in the bundle
Bundle(for: AnyClassInTheBundle.self)
answered Nov 21 '18 at 3:31
William HuWilliam Hu
6,59745267
6,59745267
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25651403%2fswift-equivalent-of-nsbundle-bundleforclassself-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown