A coworker of mine can't compile the project (I think he's on an older version of Xcode, I'm on 11.2.1). But for some reason the code below has been working and compiling for me? I would think I should be getting syntax errors because of missing return statements?
override func serviceType() -> ServiceType {
.base
}
override func operationType() -> NQOType {
.Post
}
override func doesSupportOffline() -> Bool {
false
}
Look at here: https://docs.swift.org/swift-book/LanguageGuide/Functions.html
Functions With an Implicit Return: If the entire body of the function
is a single expression, the function implicitly returns that
expression. For example, both functions below have the same behavior:
func greeting(for person: String) -> String {
"Hello, " + person + "!"
}
The entire definition of the greeting(for:) function is the greeting
message that it returns, which means it can use this shorter form.
This works now as Swift 5.1 supports implicit return statements. If the body of a function is a single statement, the return keyword can be safely omitted as this statement is considered the return statement. This also applies for closures. More info can be found at the Swift Documentation
Normally When I create a custom class I can create a function that uses that class as a parameter for example:
class Sphere{
double calculateRadius() {return 40.0;}
}
So I can then call Sphere.calculateRadius().
But with pre-existing classes like for example the String one, this can't be done. For Example. I've tried doing something like:
String.createFrom(String s){return "new";}
but it doesn't work (Android Studio doesn't even make me compile.).
Is this possible, and if it is, how could I do it in Dart? And If I created it, how would I access the object I called the method from? (the String in the example)
As of now January 2020, Dart 2.7 officially introduced extension methods.
To extend a class, this can be done:
extension NewExtension on double {
int get myMethod {
return (1/this*10).toInt();
}
}
You probably mean extension methods. That's not yet supported but the Dart team made attempts already to design such a feature and the chances are good it will be added to the language
https://github.com/dart-lang/language/issues/41
Use Extension method
Use the following syntax to create an extension:
extension <extension name> on <type> {
(<member definition>)*
}
Example :
extension MyString on String {
String addHello() {
return 'Hello : $this';
}
}
Usage :
void main() {
var name = 'Kab';
var nameWithHello = name.addHello();
print(nameWithHello);
}
Output : Hello : Kab
Your can learn more here : Dart Extension In Flutter
I am using Alamofire for network handling in swift and run into one weird error. It seems like we can't pass Method enum as parameter. [Error is on Method parameter]
private func apiRequest(method: Method, url: String, apiData: [String : AnyObject], completion:(finished: Bool, response: AnyObject?) ->Void) {
Alamofire.request(method, url, parameters: apiData).responseJSON{ response in
if let JSON = response.result.value {
completion(finished: true, response: JSON)
} else {
completion(finished: false, response:nil)
}
}
}
You have to specify the module from which to lookup object type.
Call Alamofire.Method
There is probably a name collision. To solve it, you can use the qualified name of the enum (including the module name):
private func apiRequest(method: Alamofire.Method, ...
I have also encountered this problem, because I have declared a number of the same name of the protocol:
protocol SomeProtocol {
static func someTypeMethod()
}
protocol SomeProtocol {
init(someParameter: Int)
}
protocol SomeProtocol {
var mustBeSettable: Int { get set }
var doesNotNeedToBeSettable: Int { get }
}
Had this error conflict when using "Moya" and when bridging a c framework, fixed it by implicitly adding Moya.Method module.
var method: Moya.Method {
switch self {
case .login: return .post
case .register: return .post
}
}
The type Method is declared in two imported modules. You have to specify the module from which to use the type. Use Alamofire.Method instead of Method.
Tip: If you are using the type often, you can create a type alias in your module (application):
typealias Method = Alamofire.Method
That way you will not need to prefix the type with Alamofire. any more.
While the answer to this did fix the build error; in my case, the file showing the warning was in two different frameworks so Xcode did not know where to look. This was not the intended behavior of our internal frameworks so I simply removed the copy I no longer wanted.
You may have a class declared in two or more places in your application. The error is saying that there is no conclusive way to use this class because there are a couple different places in the code it is declared.
Swift 4 and Alamofire 4.7
Replace HTTPMethod to Alamofire.HTTPMethod
I got this error because my database table name and model class name was same...Issue resolved by renaming model class name.
Change the enum type name to different &...
Use the $(inherited) flag, or
Remove the build settings from the target.
Target - > building settings- >ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES, Value type is Boolean, click on the other, change the value to $(inherited)
perform - pod update
Done
then try to run Your project , error will gone ! (I have tried in my project)
enum 'XYZ'ButtonType {
}
I managed to fix the problem by deleting the Alamofire folder in the pods project manually. Then, I do a "pod install" to reinstall the missing pods.
There are significantly less files in the Alamofire folder after doing this.
After the update of iOS 9.3 and the new update for Swift 2.2 (at least that was stated in the Xcode update changes) the code below doesn't work for me anymore.
if context[indexPath.row].dynamicType.className() == "MyClass"
it throws an exception which states:
Value of type 'AnyObject.Type' has no member 'className'
I am going through the changes which are described in swift.org but I can't find the answer of it.
Why bother with Strings when you have true compiler support in the type system itself? I recommend instead:
if context[indexPath.row].dynamicType == MyClass.Type.self {
// ...
}
Also, it's quite likely that you really mean:
if let cell = context[indexPath.row] as? MyClass {
// ...
}
unless you're intent on cutting out derived classes forever.
I don't know what className() does. It's not a known method on AnyClass.
There are a few ways you can fix this.
Use description() to get a String description of the class name:
if context[indexPath.row].dynamicType.description() == "MyClass" {
// ...
}
Or if you're using Objective-C types you can use NSClassFromString to return an AnyClass from a String:
if context[indexPath.row].dynamicType == NSClassFromString("MyClass") {
// ...
}
Swift 3:
dynamicType has been renamed to type(of:). Usage:
type(of: context[indexPath.row]) == MyClass.self
or
context[indexPath.row] is MyClass
I am usign strongloop's loopback ios sdk.Running XCode 6.3.
Objective: To create an instance of custom LBUser model class from custom LBUserRepository.
Code:
class UserProfileRepository : LBUserRepository
{
override class func repository() -> UserProfileRepository {
let repo = UserProfileRepository(className: "userprofiles")
return repo
}
}
Error:
I get an error: Cannot override 'repository' which has been marked unavailable
Why is this error coming ? is it a swift error in newer version?
References:
Apart from official documentation, I am doing it the same way as it is depicted here :
Strongloop iOS User Creation Error
and also I have even seen exact code in the test cases of obj c to swift converted code: https://github.com/Black-Tobacco/loopback-sdk-ios/tree/swift
I had the same problem and solved it like this:
class ClientRepository:LBUserRepository {
override init!(className name: String!) {
super.init(className: "Clients")
}
}