How to call a function every 3 seconds using Swift Spritekit [duplicate] - xcode

This question already has answers here:
Modify property on SKNode while moving
(2 answers)
Closed 5 years ago.
In normal swift, you can use something like this:
Foundation.Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(GameViewController.hideFunc), userInfo: nil, repeats: true)
My question is what can you replace this with to work in a Sprite kit game?

Well, you could build it pretty easily using SKActions:
let pauser = SKAction.wait(forDuration: 3.0)
let trigger = SKAction.perform(#selector(GameViewController.hideFunc, onTarget: self)
let pauseThenTrigger = SKAction.sequence([ pauser, trigger ])
let repeatForever = SKAction.repeatForever(pauseThenTrigger)
node.run( repeatForever )
This approach would have the virtue of letting you reuse this action for any nodes that would need to use it, and you can easily control the action by removing or adding the node from the scene, calling removeAllActions() on it, etc.

Related

How to drag and drop an element for xcode-ui-testing in Xcode 9

UI Test for an ios-app, developed in Xcode 8 and swift 3.2.
I'm facing a problem to deal with drag and drop after upgrading Xcode version 8 to 9
I would like to drag an element [i.e. button] and drop it to another element [i.e. on homepage].
For Xcode 8, I implemented it using below method:
let sourceElement = app.buttons["Video_Button"]
let destElement = app.scrollViews.scrollViews.images.element(boundBy: 0)
sourceElement.press(forDuration: 0.5, thenDragTo: destElement)
But the above code is not working in Xcode 9 and Swift 3.2.
Actually issue is in iOS 11.0.
In iOS 11.0 some hierarchical elements failed to tap or press or longpress.
You have to work with the elements center point to do the tap or press or longpress.
** Code snippet for the Solution of the problem is pasted below.
First create an extension method for press on a center of the element and drag this element to the destination elements center.
Extension method is below
extension XCUIElement {
func dragAndDropUsingCenterPos(forDuration duration: TimeInterval,
thenDragTo destElement: XCUIElement) {
let sourceCoordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5))
let destCorodinate: XCUICoordinate = destElement.coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5))
sourceCoordinate.press(forDuration: duration, thenDragTo: destCorodinate)
}
}
Then call the extension method like below
sourceElement.dragAndDropUsingCenterPos(forDuration: 0.5, thenDragTo: destElement)
Hope it will solve your issue. Let me know your feedback.

Trying to make a button that apears after achieving a certain score [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Im making a Single View Application in Swift and I want to create a button that appears on the screen once you get a certain score. How can I do this?
Also, if possible, I want the button to disappear after you click it.
The solution by which i solved it is timer ...
var score : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
btnTest.hidden = true
_ = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector:Selector("printer"), userInfo: nil, repeats: true)
}
func printer() {
score++
if score == 10 {
btnTest.hidden = false
}
}
you can set your updation on score variable.
there is also some other ways to do it
hope you found your solution
Thank you.
Welcome to SO. This site isn't a good fit for "teach me the basics of programming" type questions like this.
I'll give you something to start from though. Created a score property in your view controller. Implement a didSet method on your score property. Put the logic to show your button in your didSet method.
Create a button and in your code hide it in viewDidLoad then reveal it whenever your score reaches the amount you wish. But it is correct that you should be providing code and not asking just how to do something. Stack overflow is for debugging errors not asking how code should be written.

XCTest sleep() function?

I am testing an app which samples data. Part of the test I am setting up requires a few data points to be stored. I would like to do this by having XCTest execute the acquisition method followed by a sleep() function and yet another call to the acquisition method.
Though there are methods to wait for an expectation with timeout, there doesn't seem to be a simple wait()/sleep() method that simply pauses execution for specified amount of time. Any idea how I can do this using Xcode 6 and Swift?
You can use NSTimer to space out your data calls instead of locking up the app with sleep
func dataCall(timer : NSTimer) {
// get data
}
let myTimer : NSTimer = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: Selector("dataCall:"), userInfo: nil, repeats: false)
and of course you can alter those parameters to your liking and needs.

Swift Programming: Evaluate value in a string [duplicate]

This question already has answers here:
Swift - Resolving a math operation in a string
(4 answers)
Closed 8 years ago.
My question is on SWIFT/COCOA programming. I want to evaluate the content inside a string. For example, I have
var1 = "2 + 3"
Is there any inbuilt function in SWIFT/COCOA that can evaluate content in var1 and return 5. I tried what I could understand from string interpolation but that is not helping
var myNum = "45/30"
var myNum2 = "sin(30)"
var myNum3 = "\(Float(myNum) * Float(myNum2))";
error: could not find member 'convertFromStringInterpolationSegment'
Also, got another question.. How do I detect Return key pressed in SWIFT? Any trigger similar to "func applicationWillTerminate(aNotification: NSNotification) "
For your second question, assuming you're using a textField, you can use the delegate method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
This method is executed when the return key is pressed. If don't want to hide the keyboard, you need to return NO from this function at the end.
If you're using a textView, you can refer to this thread. You implement the textView:shouldChangeTextInRange:replacementText: method of UITextViewDelegate and in that check if the replacement text is \n.

Swift function swizzling / runtime

Before Swift, in Objective-C I would swizzle or hook methods in a class using <objc/runtime.h>.
If anyone has any info on the topic of modifying Swift's runtime and hooking functions like CydiaSubstrate and other libraries that helped in this area, please inform me.
I've succeed with method swizzling in Swift. This example shows how to hook description method on NSDictionary
My implementation:
extension NSDictionary {
func myDescription() -> String!{
println("Description hooked")
return "Hooooked " + myDescription();
}
}
Swizzling code:
func swizzleEmAll() {
var dict:NSDictionary = ["SuperSecret": kSecValueRef]
var method: Method = class_getInstanceMethod(object_getClass(dict), Selector.convertFromStringLiteral("description"))
println(dict.description) // Check original description
var swizzledMethod: Method = class_getInstanceMethod(object_getClass(dict), Selector.convertFromStringLiteral("myDescription"))
method_exchangeImplementations(method, swizzledMethod)
println(dict.description) //Check that swizzling works
}
Edited:
This code will work for any custom Swift class that inherits from NSObject (but will not work for classes that don't.) More examples - https://github.com/mbazaliy/MBSwizzler
You would likely be able to swizzle swift-generated classes that inherit from Objective-C classes with no problem, since they appear to use dynamic method dispatch all the time. You may be able to swizzle methods of swift-defined classes that exist in the Objective-C runtime by virtue of being passed across the bridge, but the Objective-C side methods are likely to just be proxies back across the bridge to the swift-side runtime, so it's not clear that it'd be particularly helpful to swizzle them.
"Pure" swift method calls do not appear to be dispatched dynamically via anything like objc_msgSend and it appears (from brief experimentation) that the type safety of swift is implemented at compile time, and that much of the actual type information is absent (i.e. gone) at runtime for non-class types (both of which likely contribute to the purported speed advantages of swift.)
For these reasons, I expect that meaningfully swizzling swift-only methods will be significantly harder than swizzling Objective-C methods, and will probably look a lot more like mach_override than Objective-C method swizzling.
I'm answering this question more than one year later because none of the other answers provide the definitive set of requirements for method swizzling for every kind of class.
What is described by other, while it will work flawlessly for extensions to foundation/uikit classes (like NSDictionary), will simply never work for your own Swift classes.
As described here, there is an additional requirement for method swizzling other than extending NSObject in your custom class.
The swift method you want to swizzle must be marked dynamic.
If you don't mark it, the runtime will simply continue to call the original method instead of the swizzled one, even if the method pointers appear to have been swapped correctly.
Update:
I've expanded this answer in a blog post.
I had a Xcode 7 iOS project written in Swift 2, using Cocoapods. In a specific Cocoapod, with Objective-C source, I wanted to override a short method, without forking the pod. Writing a Swift extension wouldn't work in my case.
For using method swizzling, I created a new Objective-C class in my main bundle with the method I wanted to replace/inject into the cocoapod. (Also added the bridging header)
Using mbazaliy 's solution on stackflow, I put my code similar to this into the didFinishLaunchingWithOptions in my Appdelegate:
let mySelector: Selector = "nameOfMethodToReplace"
let method: Method = class_getInstanceMethod(SomeClassInAPod.self, mySelector)
let swizzledMethod: Method = class_getInstanceMethod(SomeOtherClass.self, mySelector)
method_exchangeImplementations(method, swizzledMethod)
This worked perfectly. The difference between #mbazaliy 's code is that I didn't need to create an instance of the SomeClassInAPod class first, which in my case would have been impossible.
Note: I put the code in the Appdelegate because every other time the code runs, it exchanges the method for the original - it should only run one time.
I also needed to copy some assets that were referenced in the Pod's bundle to the main bundle.
I wouldn't do it that way, I think closures provide the answers (as they give you a chance to intercept, evaluate, and forward the invocation of the function, additionally it will be easy to extend when and if we have reflection.
http://www.swift-studies.com/blog/2014/7/13/method-swizzling-in-swift
I would like to extend the great answer provided by mbazaliy.
Another way of doing swizzling in Swift is by providing an implementation using an Objective-C block.
e.g. to replace descriptionmethod on class NSString we can write:
let originalMethod = class_getInstanceMethod(NSString.self, "description")
let impBlock : #objc_block () -> NSString =
{ () in return "Bit of a hack job!" }
let newMethodImp = imp_implementationWithBlock(unsafeBitCast(impBlock, AnyObject.self))
method_setImplementation(originalMethod, newMethodImp)
This works as of Swift 1.1.
A safe, easy, powerful and efficient hook framework for iOS (Support Swift and Objective-C). https://github.com/623637646/SwiftHook
For example, this is your class
class MyObject {
#objc dynamic func noArgsNoReturnFunc() {
}
#objc dynamic func sumFunc(a: Int, b: Int) -> Int {
return a + b
}
#objc dynamic class func classMethodNoArgsNoReturnFunc() {
}
}
The key words of methods #objc and dynamic are necessary
The class doesn't have to inherit from NSObject. If the class is written by Objective-C, Just hook it without any more effort
Perform the hook closure before executing specified instance's method.
let object = MyObject()
let token = try? hookBefore(object: object, selector: #selector(MyObject.noArgsNoReturnFunc)) {
// run your code
print("hooked!")
}
object.noArgsNoReturnFunc()
token?.cancelHook() // cancel the hook
Perform the hook closure after executing specified instance's method. And get the parameters.
let object = MyObject()
let token = try? hookAfter(object: object, selector: #selector(MyObject.sumFunc(a:b:)), closure: { a, b in
// get the arguments of the function
print("arg1 is \(a)") // arg1 is 3
print("arg2 is \(b)") // arg2 is 4
} as #convention(block) (Int, Int) -> Void)
_ = object.sumFunc(a: 3, b: 4)
token?.cancelHook() // cancel the hook
The key word #convention(block) is necessary
For hook at before and after. The closure's args have to be empty or the same as method. The return type has to be void
Totally override the mehtod for specified instance. You can call original with the same parameters or different parameters. Don't even call the original method if you want.
let object = MyObject()
let token = try? hookInstead(object: object, selector: #selector(MyObject.sumFunc(a:b:)), closure: { original, a, b in
// get the arguments of the function
print("arg1 is \(a)") // arg1 is 3
print("arg2 is \(b)") // arg2 is 4
// run original function
let result = original(a, b) // Or change the parameters: let result = original(-1, -2)
print("original result is \(result)") // result = 7
return 9
} as #convention(block) ((Int, Int) -> Int, Int, Int) -> Int)
let result = object.sumFunc(a: 3, b: 4) // result
print("hooked result is \(result)") // result = 9
token?.cancelHook() // cancel the hook
For hook with instead. The closure's first argument has to be a closure which has the same types with the method. The rest args and return type have to be the same as the method.
Perform the hook closure before executing the method of all instances of the class.
let token = try? hookBefore(targetClass: MyObject.self, selector: #selector(MyObject.noArgsNoReturnFunc)) {
// run your code
print("hooked!")
}
MyObject().noArgsNoReturnFunc()
token?.cancelHook() // cancel the hook
Perform the hook closure before executing the class method.
let token = try? hookClassMethodBefore(targetClass: MyObject.self, selector: #selector(MyObject.classMethodNoArgsNoReturnFunc)) {
// run your code
print("hooked!")
}
MyObject.classMethodNoArgsNoReturnFunc()
token?.cancelHook() // cancel the hook
After spending some time on it... Wake up this morning.... beta 6 is out and
Problem Fixed in beta6!
From release notes
"Dynamic dispatch can now call overrides of methods and properties introduced in class extensions, fixing a regression introduced in Xcode 6 beta 5. (17985819)!"

Resources