It's not a big problem, but I was a bit confused when I faced it for the first time.
This was the original declaration for an Obj C delegate method:
- (void)serialPortWasRemovedFromSystem:(ORSSerialPort *)serialPort
And when I translated it in swift it became:
func serialPortWasRemovedFromSystem(_ serialPort: ORSSerialPort)
But later Xcode showed an error and suggested me to change the name, because it was deprecated, in this one:
func serialPortWasRemoved(fromSystem serialPort: ORSSerialPort)
Why did they change this delegate name so many times? Can you tell me why? Thank you! ~
Because that, in large part, is what Swift 3 is. The Objective-C APIs are "renamified" to make their names terser and more Swift-like.
To learn more, read this and the other two documents to which it links.
Related
I want to use kind of SwiftUI-like functional style of configuring the UI, for example by providing [a global] method hide(view: UIView) instead of writing view.isHidden = true.
Now this works fine but for reverse logic I want to have the global method show() but within context of an UIViewController it conflicts with the class method show(sender: Any).
Is there any language trick I can you in order not to have to write MyAppName.show() each time I use it?
Your title is a bit inaccurate. You already know how to disambiguate it. The question is about how to do so without repeating the module name. Unfortunately, I don't think there's any such way.
I found out that Swift compiler seems to be overreacting, as one method has 2 arguments and my method has 1 argument. What's the reason to write "nearly matches" and breaking compilation is unclear to me.
However, I found the solution:
extension UIViewController{
func show(_ view: UIView) { view.isHidden = false }
}
Does anybody know why the ZoomingPDFViewer Apple Sample Code project no longer works? It was working prior to the iOS 10 release but now it keeps returning a unrecognized selector error when calling [PDFScrollView setPDFPage:].
It seems like the custom classes set in the storyboard are no longer being instantiated.
I had the same issue. It seems that the auto conversion to Swift 3 doesn't work well with IBs.
There are two options:
a) You can set the argument label--i.e.the parameter name to be shown externally--to _. For example,
#IBAction func setPDFPage(_ sender: AnyObject) {
// ...
}
b) You can remove the IBAction connection in the IB and re-set it. Notice instead of the previous setPDFPage(sender:) the connection will say setPDFPageWithSender(sender:) or something like that. (I didn't actually try it with the code in question, but writing from experience here.)
The same is true for segues.
I need to make a bezier path, I have the CGPath
the only information I found was this:
Declaration
SWIFT
convenience init(CGPath CGPath: CGPath)
the first is written normal, the second in Italics and the third purple in the developers library
How should I use it? Please give me an example.
If you have a valid CGPath, just do this:
bezier_path = UIBezierPath(CGPath: your_cg_path)
The vast majority of the yWithX type methods from Objective-C were replaced with init methods in Swift. This Apple documentation has more information on the subject.
first post!
i've been working with swift for a while now, but decided to move some of my code to using shared variables and objects. I'm having a fundamental issue with being able to reference appDelegate.SharedApplication().delegate. Even with a basic test application (to see if I could see any fundamental problems), I cannot get a reference to the shared variable -
// AppDelegate.swift
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let info: String = "test"
}
trying to add - "let ref = UIApplication.sharedApplication().delegate as appDelegate" in another class complains of use of undeclared type app delegate.
removing that cast allows that, but once I try to create another variable reference to the info string, complains of no member named ref.
this seems such an obvious and confusing issue that I thought it time to ask for an answer :) all i want is to be able to use the appdelegate for controlling cllocationmanager and storing the return data in a variable for a view controller class to refer to.
xcode 6.1.1
cheers
Maybe you are confusing the compiler because you are not using the proper case (upper or lower) when referring to variables or classes.
A class starts with an UpperCase letter.
Any variable starts with a lowerCase letter.
While this is not a rule for variables, it is a well-heeded convention.
Thus:
let stringFromDelegate =
(UIApplication.sharedApplication() as AppDelegate).info
I have a data class that starts with a bool value:
- (id)initWithBool:(BOOL)hasSomeValue anotherVariable:(var type)var ...
But this is unreadable when instantiating in XCode, because "hasSomeValue" is not shown when auto-complete appears.
-(id)initWithHasSomeValue:(BOOL)hasSomeValue anotherVariable:(var type)var ...
Is also rather clunky. I've done some searching but can't seem to find Apple's recommended specs on this.
EDIT: A pointer to an example by Apple would help very much. I still haven't managed to find one.
You'd really want something that tells you WHAT the BOOL actually represents
- (id)initWithBool:(BOOL)hasSomeValue ...
- (id)initWithHasSomeValue:(BOOL)hasSomeValue ...
would be comparable to
- (id)initWithString:(NSString*)string ...
if you were initiating a BOOL (which clearly is nonsense), except with [NSNumber numberWithBool:]
Generally speaking, the name of the parameter should describe its semantics, which "bool" doesn't really do. E.g. "initWithName:" is better than "initWithString:".
Even more generally speaking, you have a data class that takes a boolean as it's first parameter? Maybe you need to rethink why it's there at all?
Have you added the custom init method to your header file?
Although it will compile (with a warning) and run fine, the custom init method will only appear in auto complete if it appears in the header file.