This question already has answers here:
Filtering user-launched applications (Mac OS X)
(2 answers)
Closed last month.
I am trying to have my OS X Application detect if a certain program is running but NSRunningApplications isn't working for me. This is my code:
import AppKit
class func sharedWorkspace() -> NSWorkspace
{
var runningApplications: [NSRunningApplications] {get}
}
The error I'm getting is
Expected '{' to start getter definition.
as well as
Missing return in a function expected to return 'NSWorkspace.
Any ideas?
You've got to use NSWorkspace.sharedWorkspace().runningApplications itself:
for app in NSWorkspace.sharedWorkspace().runningApplications {
if let name = app.localizedName {
print(name)
}
}
You can get the running applications names in an array with a simple map (or flatMap here to manage Optionals):
let names = NSWorkspace.sharedWorkspace().runningApplications.flatMap { $0.localizedName }
print(names)
In Swift 5+ the syntax changed to:
NSWorkspace.shared.runningApplications
Related
This question already has answers here:
Change in Xcode 10 Playgrounds Variable Initialization change? Are Xcode 10 Playgrounds an interpreter?
(2 answers)
Closed 4 years ago.
Instead of using the ternary operator, I coded a if-else block which doesn't seem to compile... Didn't understand the bug exactly, why isn't it working? Error line is :
error: MyPlayground2.playground:6:1: error: variables currently must have an initial value when entered at the top level of the REPL
var parentAge: Int
And my code:
import UIKit
var parent: String = "mom"
var parentAge: Int
let optOne = 39
let optTwo = 43
if parent == "mom" {
parentAge = optOne
print(parentAge)
} else {
parentAge = optTwo
print(parentAge)
}
error: variables currently must have an initial value when entered at the top level of the REPL
You should either initialise it with optional or assign an initial value if not optional.
var parentAge: Int?
var parentAge: Int = 0
Both of above representation are correct.
This question already has answers here:
.toInt() removed in Swift 2?
(5 answers)
Closed 6 years ago.
I'm making a mock remote control Iphone app using swift and Xcode is not letting me use .toInt() on the text of a UILabel. I'm trying to convert the text from a label into an integer and I'm not sure how to do this. Can anybody help me out? Thanks.
Here's my code:
#IBAction func channelInc(_ sender: UIButton) {
var chnl = channel.text!.toInt()
if (chnl!+1 > 99) {
} else {
let newChnl = chnl!+1
channel.text = "\(String(newChnl))"
}
}
There is not method on the String class called toInt(). The way to do it in swift is to use the initializer for the Int class.
let number = Int("5")
// returns a Int? (optional)
Try
let chnl:Int? = Int(channel.text!)
I am trying to make my first app and I am stuck. I don't know how to rewrite this piece of code from an earlier version of Swift in Swift 2.
func rowCheck(value value:Int) -> (location :String,pattern :String)?{
let acceptableFinds = ["101","110","011"]
var findFunc = [checkGornjiR,checkSrednjiR,checkDonjiR,checkLevuK,checkSrednjuK,checkDesnuK,checkLevuD,checkDesnuD]
for algorithm in findFunc{
let algorithmResults = algorithm(value:value)
if find(acceptableFinds,algorithmResults.pattern) { // Error on this line
return algorithmResults
}
}
return nil
In "if" line I am getting error:
"Optional type 'C.index?' cannot be used as a boolean; test for '!=nil' instead
A bit of help would be nice! Thanks!
you van use contains which return Bool value like this:
if acceptableFinds.contains(algorithmResults.pattern) {
This question already has answers here:
stringByAppendingPathComponent is unavailable
(11 answers)
Closed 7 years ago.
I have just installed Xcode 7 with the new Swift 2, and I now have 50+ errors saying that "stringByAppendingPathComponent" is unavailable, and that I should use "URLByAppendingPathComponent" instead. I have been setting all of my texture properties like so:
let dropTexture = SKTexture(image: UIImage(
contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent(
"P04_rainDrop1.png"))!)
I have been doing this so they do not stay in memory when the SKScene is changed and it has been working perfectly. However directly replacing "URLByAppendingPathComponent" does not fix the errors.
How can I change this to fix the error and get the same SKTexture?
All you have to do is cast to NSString to recover stringByAppendingPathComponent, like this:
let dropTexture = SKTexture(image: UIImage(
contentsOfFile:(NSBundle.mainBundle().resourcePath! as NSString).stringByAppendingPathComponent(
"P04_rainDrop1.png"))!)
As Leo Dabus rightly says, you can save yourself from all that casting by adding an extension to String. However, you should not, as he suggests, call NSString(string:), which generates an extra string. Just cast:
extension String {
func stringByAppendingPathComponent(pathComponent: String) -> String {
return (self as NSString).stringByAppendingPathComponent(pathComponent)
}
}
So I'm following a tutorial form Lynda.com for making a iOS app with Swift and when I plug this line of code in, it's throwing me errors:
guard let text:String = addressBar.text else
The error I get is:
Consecutive statements on line must be separated by ';'
Once I have Xcode fix it, these are the errors I get:
Expected expression.
Use of unresolved identifier 'guard'.
Expression resolves to an unused function.
Braced block of statements is an unused closure.
I'm really new to Xcode and Swift so any help would be awesome! Thanks!
Because you using outdated xcode and swift language. Latest version is xcode 7 and swift 2.
https://developer.apple.com/xcode/
May be you are using a wrong version of Xcode(version 7.0)
Try it too:
Be certain you are using guard statement in the right conditions. E.g:
class AddressBar {
var text: String? = ""
}
var addressBar = AddressBar()
addressBar.text = nil
//addressBar.text = "text"
func test() {
guard let _text: String = addressBar.text else {
print("Nothing")
return
}
print("I reach this point")
}
test()