Take a look at for instance the value type WWDC videos linked above, or for example this blog post as an introduction. Briefly, using value types you gain good local reasoning, often avoid dynamic memory allocation and reference counting overhead though not universally so — structs with reference types as fields being the caveat.
One reason to subclass NSObject is if you need to save some data. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Swift 3: subclassing NSObject or not? Ask Question. Asked 5 years, 1 month ago. Active 3 years ago. Viewed 13k times. Improve this question. Community Bot 1 1 1 silver badge. AppsDev AppsDev Add a comment. Call the one line macro whenever you need singleton object for a class.
Example: CommonMacros. I've found the solution! It connects the power of quartz2d and simplicity of UIGraphics. Marus Nebunu. Only authorized users can answer the question. Please sign in first, or register a free account. Not the answer you're looking for? To fix this problem, we need to create an init method that accepts two parameters, one for the name and one for the image. We'll then save that to the object so that both variables have a value, and Swift is happy. Our custom class is done; it's just a simple data store for now.
If you're the curious type, you might wonder why I used a class here rather than a struct. This question is even more pressing once you know that structs have an automatic initializer method made for them that looks exactly like ours.
Well, the answer is: you'll have to wait and see. All will become clear in project 12! With that custom class done, we can start to make our project much more useful: every time a picture is imported, we can create a Person object for it and add it to an array to be shown in the collection view. Every time we add a new person, we need to create a new Person object with their details.
This is as easy as modifying our initial image picker success method so that it creates a Person object, adds it to our people array, then reloads the collection view. Put this code before the call to dismiss :.
That stores the image name in the Person object and gives them a default name of "Unknown", before reloading the collection view. Can you spot the problem?
0コメント