Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Is it better to declare an ivar in a class extension?
Or is it only a developer preference?
As the name suggest it is an extension to the functionality of class. You may add extra behavior to the class using extension. Adding member variables or stored properties have quite a bit problems viz:
When it is possible to add data member in extension the original class is not aware of the added data member. This leads to problem of allocation for such members while creating object for such classes.
It could also create problem with initialization and de-initialization of data members as these are not known to original init() or deinit() methods.
Adding data member may alter the very nature of class in terms of its behavior.
Currently you are unable to add instance variables to class extensions. This is true for both swift and Obj-C. See this question: Defining a property in iOS class extension.
If you mean, by adding a iVar to the interface extension:
#interface MyClass(){
MyIvar *ivar
}
well, thats a bit of a matter of choice and convenience. In general you should limit your public interface to a minimal set of properties/methods that allow the user to interact with the class in the way you design/expect. While your code will work fine using either method, exposing more properties/functions can result in more problems as consuming classes may uses properties/functions in ways unplanned for or unexpected.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am new to Swift development and wanted to ask a question related to Swift files.
If I am making a project is it a good or general practice to use only one struct or class in one file? Do you need to create a different .swift file for every new struct or class ?
It's common to make a new file per type (struct/class/enum) with the file named for the type, but there are exceptions to the rule. If the type definition is small, and closely related to some other type, then it can be easier to read if it's in the same file with the other type definition - that's quite common with enums. Or if the type is nested in an enclosing type, then it's often written inline. But this is a matter of personal preference.
It's not necessary. But create a different .swift file for every new struct or class help you more easy to maintain source code when your codebase is large
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm interested in how to properly abstract Firebase in order to decouple it with my app in case I want to switch back ends in the future.
Right now I have a single class with static methods that access the Realtime Database and Storage. I call these static methods throughout the app.
Is this the best way to use Firebase in a production environment? My app is written in Swift.
You can create a Wrapper class
class YourWrapperClass: NSObject {
}
Import the frameworks like Firebase that you want to use with this wrapper.
import Firebase
Create the methods with the use of Completion Handler/Closures/Blocks
That’s It. By this way you can use the code reusability.
Whenever you want to stop using Firebase, you will have to just stop calling methods from this wrapper class & implement alternative methods you want to call/use instead. Hope this will help.
I think the best way here is to have struct with static members for each main node in your Firebase database and also separate models for each main folder of Firebase storage. It should look like API's. You can change it in the future without any problems.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have read several articles discussing pros an cons about singleton patterns. But I would like to know:
Is it advisable to have multiple singletons in an iOS App?
what are the pros and cons...?
Currently I am having only one singleton globally and holding strong references of other necessary properties including custom composite classes. But the idea sounds something strange for me for an example, accidentally I can create several instance of a custom composite class which I don't want.
You should have as many singletons as you need. Take a look at Cocos2d - it contains a fair amount of them: CCDirector, CCTextureCache, CCSpriteFrameCache and so on. There's no limit on singletons, say 5. If it's convenient for you to have one single center class for a certain kind of operations (like accessing network or a database or whatever) and you never need a second instance of this class then feel free to make it a singleton.
It depends on your requirement.
You can have multiple singleton classes or objects.
The singleton object will be alive till your application quits.
For memory managing concern, it'll be very difficult if you have multiple singleton objects(You can't release these singleton objects, when a memory warning raises).
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Just something I've noticed: when you create new files in XCode that are not a subclass of a controller, there is no #interface in the .m file by default. I'm going to assume that's done intentionally - I'm curious as to why that is
I was thinking that possibly its because they're making the assumption that you're going to want most of your properties to be publicly accessible for parent controllers and the likes?
I've tried researching this to no avail - help me out SO! :D
I think I'd generally be wary of trying to draw any conclusions from Apple's template files - a lot of their sample projects and project templates don't really follow best practices. For example, if you create a project with Core Data, the template has all of the Core Data code within the app delegate - somewhere it really doesn't belong.
On the topic of including an #interface class extension within the .m file - I usually have these in most classes, and keep all properties / methods private unless they definitely need to be visible to another class.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have gone through this
for getting the idea of anonymous category.
But I found that I was not getting any warning when I have not declared the method while working with xcode 5.
Does anonymous category concept still work in xcode 5.
as per this post Objective-C class extension
Methods in class extensions
From Xcode 4.4 (I believe), the compiler is clever enough to determine which methods are meant to be private within that implementation, removing the need to declare them elsewhere.
difference between category and class extension
the main difference between categories and extensions is that the extension expects you to implement the methods inside your main implementation, whereas with a category, it can be in another implementation. It also seems that people are using extensions mainly for private methods.
"Anonymous category" is called class extention. In modern Objective-C you do not need forward declaration of methods so declaring them in class extentions is not necessary.
Whether or not you see warnings depends on your compiler settings. And yes, they do still work in Xcode 5, they are a first-class feature of the Objective-C language.
Please note that the more common name for anonymous categories is a class extension.