Forwarding an error in Swift
Yes: don't wrap it in a do ... catch block.
func func2() throws{
// proof something
throw Error.Error1
}
func func1()throws{
try func2()
}
Closure forwarding without evaluating
Will my code above evaluate the closure in
MyLogDebug
?
No. The call message()
will be wrapped in a closure by the @autoclosure
in MyLogMessage
and only evaluated when message()
is subsequently called in MyLogMessage
.
Here is a little standalone example to play with:
func DDLogDebug(_ message: String) {
print("in DDLogDebug")
print(message)
print("leaving DDLogDebug")
}
public func MyLogDebug(_ message: @autoclosure () -> String) {
// some code
print("in MyLogDebug")
MyLogMessage(message(), flag: true)
print("leaving MyLogDebug")
}
public func MyLogMessage(_ message: @autoclosure () -> String, flag: Bool) {
// some code
print("in MyLogMessage")
if (flag) {
DDLogDebug(message())
}
print("leaving MyLogMessage")
}
MyLogDebug({ print("evaluated"); return "error message" }())
Output:
in MyLogDebug
in MyLogMessage
evaluated
in DDLogDebug
error message
leaving DDLogDebug
leaving MyLogMessage
leaving MyLogDebug
Note that the initial closure passed to MyLogDebug
isn't evaluated until the DDLogDebug(message())
in MyLogMessage()
.
If you change the flag
to false
in the call to MyLogMessage()
, the initial closure will never be evaluated.
How to redirect all 404 errors
You may be hitting a couple of issues here. First, the abort error could be being thrown in a future, in which case you need to add a catchMap
to the next.respond(to:)
call to catch that case.
It also may not throw (though this is unlikely), so you can try unwrapping the response and checking the status code.
Have you put a breakpoint in to see if it ever hits it etc?
Swift 2 syntax error with direction .Forward
You should make sure that your viewControllers not optional Type. Such as:
var viewControllers:[UIViewController] = [VC1,VC2]
self.pageViewController.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
Receiver '**' for class message is a forward declaration Error. Swift Static Library use in Objective-C
My problem was with '(ProductName)-Swift.h'
If you look at how Swift Code is used in Objective-C, many articles say to import (ProductName)-Swift.h. So I only added the project header that I want to apply, but I also need to add the product header made from the library.
My problem was simple, but it took me a long time to figure it out. The error was not found 'Class' and 'func' in Swift static library. My workaround was resolved using the (LibraryProductName)-Swift.h of the library I created, rather than the (ProductName)-Swift.h of the project you are working on.
If you refer to the address below, you can prevent the error that occurred in advance.
https://medium.com/@mail2ashislaha/swift-objective-c-interoperability-static-libraries-modulemap-etc-39caa77ce1fc
swift: flip forward and backward action
First you must have an active pageVC to be able to navigate to a specific VC , also instead of
pageController.setViewControllers(contentControllers, direction: .forward, animated: true, completion: nil)
You must set only the vc you want to navigate to like
pageController.setViewControllers([firstController], direction: .forward, animated: true, completion: nil)
and keep track of it's index , so when the user scrolls again the dataSource methods are called to load the correct VC , I have created a demo Here that does the same job
Getting Error Receiver for class message is a forward declaration
Finally I got solution. I did following changes. Here below is to do list in your Framework.
Swift classes usage in Objective C classes
- Use Open Keyword before class name in Swift
- Use @objc Keyword before Open Keyword of Swift class
- Import all header of objective c classes in Umbrella file
YourProject.h those are consuming Swift Classes. - Use
#import <YourProjectName/YourProjectName-Swift.h>
in Objective
c
I followed Apple's Mix and Match approach.
Note: sometimes the <YourProjectName/YourProjectName-Swift.h>
file can not be imported into headers, but only into .m
files. FYI, this file is created automatically by the compiler. If you search your project for it, you won't be able to find it, but if you ⌘ click on it (as a file imported in your code) in a file that is in your target, Xcode will (should) open it and show you all the Objective-C class interfaces for your Swift classes. To see the name for the Swift -> Objective-C module header, go to your target's Build Settings and search for $(SWIFT_MODULE_NAME)-Swift.h
or simply -Swift
.
rethrows for class/struct instance method in swift error handling
As said here: https://stackoverflow.com/a/33350601/1370336, in your "MiddleParser" struct, you don't have to wrap the call in a do
- catch
block (since your function is marked with throws
), just use:
inners.append(try self.innerParser.parse(innerJson))
Why is forwarding variadic parameters invalid?
When you call foo
, the compiler expects a series of arguments, each of which must be an Int
.
In the body of foo2
, bar2
summarises all the passed arguments and actually has the type Int[]
for all practical purposes. Thus, you cannot pass it directly to foo
— as foo
wants Int
arguments, and not an Int[]
.
As for a solution to this: see my answer to this question.
Related Topics
Programmatically Creating Constraints Bound to View Controller Margins
How to Simulate Mouse Click from MAC App to Other Application
When and How to Use @Noreturn Attribute in Swift
Best Practice for Swift Methods That Can Return or Error
Swiftui List Empty State View/Modifier
Can the Conversion of a String to Data with Utf-8 Encoding Ever Fail
How to Make Physics Bodies Stick to Nodes Anchor Points
Format Realtime Stopwatch Timer to the Hundredth Using Swift
Multiple Bottom Sheets - the Content Doesn't Load Swiftui
Convert Generic Free Function into Array Extension
How to Reproduce This Xcode Blue Drag Line
Dynamically Change Text of Realitykit Entity
Method' Is Ambiguous for Type Lookup in This Context, Error in Alamofire