Hello friends I am getting a strange error while creating a model class Though i have created the class it shows an error
any suggestion !!
Thank you
Here is the code
import Foundation
class ComboModel {
private var _ComboItemId: String!
private var _ComboMainCategory: String!
private var _ComboCategoryId: String!
}
var ComboItemId : String {
return _ComboItemId // Error Use of unresolved identifier '_ComboItemId'
}
init(ComboItemId : String) {
self._ComboItemId = ComboItemId // Error Initializers may only be declared within a type
}
This is a simple user mistake. You've got a class with private variables
class ComboModel {
private var _ComboItemId: String!
private var _ComboMainCategory: String!
private var _ComboCategoryId: String!
} // your class ends here!!!
Now you're trying to access ComboModel classes private variable _ComboItemId.
var ComboItemId : String {
return _ComboItemId // Error Use of unresolved identifier '_ComboItemId'
}
So, this is probably what you're looking for:
class ComboModel {
private var _ComboItemId: String!
private var _ComboMainCategory: String!
private var _ComboCategoryId: String!
var ComboItemId : String {
return _ComboItemId
}
init(ComboItemId : String) {
self._ComboItemId = ComboItemId
}
}
Related
I have a query in Swift 3.0. I am calling a web service and getting an array of contacts from the server. I am creating a class Contacts:
Class Contacts: NSObject {
var address : String?
var email : String?
var gender : String?
var name : String?
var id : String?
var phone : String?
//var array to store class object
var arrayOfContacts = [Contacts]()
override init() {
super.init()
}
}
I need to access all the objects store in arrayOfContacts from somewhere in my code.
How should I create such a functionality or I am on the wrong way?
Two ways
way 1:
Declare your arrayOfContacts as static
class Contacts: NSObject {
var address : String?
var email : String?
var gender : String?
var name : String?
var id : String?
var phone : String?
//var array to store class object
static var arrayOfContacts = [Contacts]()
override init() {
super.init()
}
}
And access it as
print(Contacts.arrayOfContacts)
Way 2:
If you want your contacts to be singleton use
class Contacts: NSObject {
var address : String?
var email : String?
var gender : String?
var name : String?
var id : String?
var phone : String?
var arrayOfContacts = [Contacts]()
static var sharedInstance = Contacts()
private override init() {
super.init()
}
}
Then access it as
Contacts.sharedInstance.arrayOfContacts
Hope it helps
actually you can create an array of objects in same class but you must not it in same class, so I provide code how to do it
Class Contacts: NSObject {
var address : String?
var email : String?
var gender : String?
var name : String?
var id : String?
var phone : String?
override init() {
super.init()
}
static func arrayOfObject(_ array: NSArray)-> [Contacts] {
var contactsArr: [Contacts] = []
for data in array {
let cont = Contacts(...)//init all properties
contactsArr.append(cont)
}
return contactsArr
}
}
and call this method where you get your data
Contacts.arrayOfObject(<array of data>)
You can use Singleton pattern for this.
Play with it in playground.
import Foundation
class Contacts: NSObject {
var address : String?
var email : String?
var gender : String?
var name : String?
var id : String?
var phone : String?
override init() {
super.init()
ContactSingleton.shared.arrayOfContacts.append(self)
}
}
final class ContactSingleton {
// MARK: Properties
internal static let shared = ContactSingleton()
internal var arrayOfContacts: [Contacts] = []
// MARK: - Initializer
private init() { }
}
let contact1 = Contacts()
print(ContactSingleton.shared.arrayOfContacts.count) // 1
let contact2 = Contacts()
let contact3 = Contacts()
print(ContactSingleton.shared.arrayOfContacts.count) // 3
class ShareData {
class var sharedInstance: ShareData {
struct Static {
static var instance: ShareData?
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token) {
Static.instance = ShareData()
}
return Static.instance!
}
var someString : String! //Some String
var selectedTheme : AnyObject! //Some Object
var someBoolValue : Bool!
}
This is my singleton design.However , I want to know how I can clear all its data as and when required?
Also can i have more than one singleton Class??
Since you've only got 3 properties on your singleton it would be far easier just to set up a method that nils each property in turn.
Once you start getting in to how to destroy and recreate your singleton, you get in to the realm of do you actually even want a singleton or should you just be using a regular object.
You are creating a Singleton with the syntax available in... 2014
Today there's a better syntax to define a Singleton class
final class SharedData {
static let sharedInstance = SharedData()
private init() { }
var someString: String?
var selectedTheme: AnyObject?
var someBoolValue: Bool?
func clear() {
someString = nil
selectedTheme = nil
someBoolValue = nil
}
}
As you can see I also added the clearData() method you were looking for.
I need to use struct in my custom framework, how can the following be done?
Here my framework:
MyFramework.framework
public class SomeClass : NSObject {
public struct myDetails {
public var firstName: String?
public var lastName: String?
public init(fName: String? LastName lName: String?) {
self.firstName = fName
self.lastName = lName
}//init end
}// struct end
public func fillStruct(inout myStruct: myDetails) {
myStruct.firstName = "John"
myStruct.lastName = "Devid"
}
}// class end
Here's my Application:
// ViewController.swift
import MyFramework
var refObj: SomeClass?
override func ViewDidLoad() {
refObj = SomeClass()
let tempStruct = refObj.myDetails(fName: nil LastName lName: nil) // I CANT ABLE ACCESS LIKE THIS.
refObj.fillStruct(&tempStruct)
}
What I want, is my application to send the data to the struct and my framework to fill the struct and finish the function.
How can this be achieved?
I'm working on my first Swift based project (xcode 6.3.2) and don't seem to get a simple generics construct to build.
The following seems to fail to compile on xcode 6.3.2 (compiler exit with code 1):
// with :AnyObject constraint => compiler crash
class GenericsTest : MyDelegate{
static let instance = GenericsTest()
private init(){
var wrapper = MyWrapper<MyDelegate>(inner: self)
if let md = wrapper.inner {
println("delegate value \(md.someString)")
}
}
// MyDelegate impl
var someString = "my delegate impl"
}
protocol MyDelegate : class {
var someString : String {get set}
}
class MyWrapper<T: AnyObject>{
private (set) var inner : T?
init(inner : T){
self.inner = inner
}
}
It looks like the Swift compiler does not like the :AnyObject type constraint of MyWrapper; the following code compiles and runs without any problem:
// no :AnyObject constraint
class GenericsTest : MyDelegate{
static let instance = GenericsTest()
private init(){
var wrapper = MyWrapper<MyDelegate>(inner: self)
if let md = wrapper.inner {
println("delegate value \(md.someString)")
}
}
// MyDelegate impl
var someString = "my delegate impl"
}
protocol MyDelegate : class {
var someString : String {get set}
}
class MyWrapper<T>{
private (set) var inner : T?
init(inner : T){
self.inner = inner
}
}
I need the :AnyObject type constraint because I'm actually trying to create a weak wrapper, which requires this constraint, like so:
// with weak wrapper and obligatory :AnyObject constraint (compiler crash)
class GenericsTest : MyDelegate{
static let instance = GenericsTest()
private init(){
var wrapper = MyWrapper<MyDelegate>(inner: self)
if let md = wrapper.inner {
println("delegate value : \(md.someString)")
}
}
// MyDelegate impl
var someString = "my delegate impl"
}
protocol MyDelegate : class {
var someString : String {get set}
}
class MyWrapper<T: AnyObject>{
weak private (set) var inner : T?
init(inner : T){
self.inner = inner
}
}
Anybody any idea how to work around this issue?
I am using XCode 6.3.1 with Parse 1.7.2.
I defined the following PFObject subclass:
class MyClass: PFObject, PFSubclassing {
#NSManaged var date:NSDate!
#NSManaged var title:String!
#NSManaged var device:String!
#NSManaged var subscribers:NSArray!
#NSManaged var numberOfPictures:NSNumber!
override class func initialize() {
struct Static {
static var onceToken : dispatch_once_t = 0;
}
dispatch_once(&Static.onceToken) {
self.registerSubclass()
}
}
static func parseClassName() -> String {
return "MyClass"
}
}
But when I try to instantiate this class later on in some other code:
var myObject = MyClass()
The compiler gives me an error, saying that I should specify a className parameter to the MyClass constructor.
Did I miss something here?
Parse SDK 1.7.3 seems to solve this bug.
The way you handle did not give it a class name when you initialize, it should be something like this:
var myObject = MyClass("MyClass")