मैं अपनी परियोजना में MKAnnotationView
का उप-वर्ग बना रहा हूं। इसे सबव्यूज़ को संग्रहीत करने के लिए दो गुण होने की आवश्यकता है जिन्हें मुझे शुरुआत में कहीं भी शुरू करने की आवश्यकता है।स्विफ्ट में एमकेएनोटेशन व्यू सबक्लास के प्रारंभिकरणकर्ताओं को क्या प्रारंभ करना चाहिए?
MKAnnotationView
इसके प्रलेखन, initWithAnnotation:reuseIdentifier:
में डाला गया प्रारंभकर्ता है, इसलिए मुझे लगता है मैं बस उस ओवरराइड था लगा:
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
override init!(annotation: MKAnnotation!, reuseIdentifier: String!) {
innerCircle = ...
outerCircle = ...
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
...
}
लेकिन यह एक क्रम अपवाद का कारण बनता है:
fatal error: use of unimplemented initializer 'init(frame:)' for class 'PulsatingDotMarker'
ठीक है, तो मैं अनुमान initWithAnnotation:reuseIdentifier:
आंतरिक रूप से initWithFrame:
पर कॉल करता है, इसलिए शायद यह है कि मुझे इसके बजाय ओवरराइड करना चाहिए। के कि कोशिश करते हैं:
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
override init(frame: CGRect) {
innerCircle = ...
outerCircle = ...
super.init(frame: frame)
}
...
}
जब एनोटेशन दृश्य वस्तु बनाने हालांकि यह एक संकलन त्रुटि होती है:
Extra argument 'reuseIdentifier' in call
हम्म, इसलिए यदि मैं() की आवश्यकता प्रारंभकर्ता initWithFrame:
लागू, यह अब डिफ़ॉल्ट प्रारंभकर्ता खो देता है initWithAnnotation:reuseIdentifier:
?
शायद अगर मैंने initWithAnnotation:reuseIdentifier:
का ओवरराइड जोड़ा जो केवल super
पर कॉल करता है तो यह फिर से उपलब्ध होगा, क्या यह काम करेगा?
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
init!(annotation: MKAnnotation!, reuseIdentifier: String!) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
override init(frame: CGRect) {
innerCircle = ...
outerCircle = ...
super.init(frame: frame)
}
...
}
नहीं, अभी भी नहीं अच्छा - संकलन त्रुटि:
Property 'self.innerCircle' not initialized at super.init call
ठीक है, क्या हुआ अगर मैं एक initWithFrame:
था, लेकिन initWithAnnotation:reuseIdentifier:
में subviews प्रारंभ? (? लेकिन फिर क्या हुआ अगर किसी को सिर्फ सीधे initWithFrame:
कॉल ...)
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
init!(annotation: MKAnnotation!, reuseIdentifier: String!) {
innerCircle = ...
outerCircle = ...
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
...
}
नहीं आश्चर्यजनक रूप से, स्विफ्ट मुझे उस से मुझे बता द्वारा सुरक्षा करता है:
Property 'self.innerCircle' not initialized at super.init call
(initWithFrame:
में इस समय)।
तो मुझे क्या करना चाहिए? मैं यहाँ और वहां दोनों सबव्यूव्यू नहीं बना सकता, है ना?
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
init!(annotation: MKAnnotation!, reuseIdentifier: String!) {
innerCircle = ...
outerCircle = ...
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
override init(frame: CGRect) {
innerCircle = ...
outerCircle = ...
super.init(frame: frame)
}
...
}
गलत फिर से, यह वास्तव में काम करता है - भले ही मैं एक ही वस्तु में दो बार एक निरंतर संपत्ति बताए कर रहा हूँ (!)।
क्या किसी को पता है कि यह ठीक से कैसे किया जाना चाहिए?
(नोट: वर्ग भी एक आवश्यक initWithCoder:
प्रारंभकर्ता कि सिर्फ पहला उदाहरण से fatalError
कॉल शामिल है, लेकिन वस्तु एक स्टोरीबोर्ड से कभी नहीं बनाई गई है।)
क्या होगा अगर subviews घोषित किया गया है 'var innerCircle: UIView!' के रूप में? यह "आवश्यक init (xxx)" त्रुटियों से बच सकता है। – Anna
यह वही है जो मैंने किया है, लेकिन यह एक हैक की तरह लगता है - एक संपत्ति को शून्य और परिवर्तनीय के रूप में चिह्नित करता है, भले ही यह कंपाइलर को खुश करने के लिए नॉन-नल और स्थिर होना चाहिए ... –
मैंने इसी तरह की समस्याओं पर कुछ समय बिताया, और एक टेस्ट प्रोजेक्ट बनाया जहां मुझे लगता है कि मैं इस कारण को काम करने में कामयाब रहा हूं (उद्देश्य सी क्लास '[स्वयं initWith ...]' विधि को अपने सार्वजनिक 'init' विधि में कॉल कर रहा है), लेकिन दुर्भाग्य से अभी तक कोई समाधान नहीं है - http://stackoverflow.com/questions/31161143/non-designated-initialiser-inheritance-from-objective-c-classes – Rupert