How to resolve expected declaration error in swift? - swift

I am new to swift and am trying to build an app. I am receiving "Expected Declaration" error on Xcode when I type the following code.
.edgesIgnoringSafeArea(.bottom)
Please guide me on how to resolve it. Thanks

A declaration is one of the things listed in https://docs.swift.org/swift-book/ReferenceManual/Declarations.html
If the compiler is saying it's expecting a declaration, it means you have written something that isn't a declaration in some place in the code where only a declaration is allowed.
In your case .edgesIgnoringSafeArea is a method on SwiftUI View type, so it would only be valid to put that right after a SwiftUI view, not on its own. It might be because you have brackets in the wrong place.

Related

String is not a String

I can't grasp what's going on. There are two issues here.
"Can't convert type 'String' to expected type 'String'" //am I blind, I am checking this again and again... but those are both the same Strings. And this error disappear when I type in some hardcoded String.
Combination of errors at the top which clearly says: there can not be Strings used as Keys in Dictionaries and error at the bottom where UserDefaults clearly return type which is forbidden by compilator few lines above.
I don't even know how to ask clearly. But where to start? In second thing I can do some workarounds, some ugly hacks, but this "String" is not "String"... ehmm... maybe someone can point me to some place where someone had similar issue?
EDITED:
public class PersistentDictionary<String: Codable>
this is trouble. As someone pointed out I am creating new Generic Type which inherits from Codable. It's like . Not sure how that landed there - it was never my intention to make it generic, but...
In this very case it was declaration of class:
public class PersistentDictionary<String: Codable> {
//see comment which explain it in details

<unknown>:0: error: type 'Key' constrained to non-protocol type 'String'

Migrating to Swift 3 from 2.3 and am running into this issue. The error is traceable to a view controller.
I do not see any extensions/protocols which would require a 'Key' constrained to 'String'.
I've tried to comment out code that might be causing the error, and have had strange results - ie removing an empty viewDidLoad() made the error appear in another class.
I'll update the thread if I make progress.
Key is a type inside a structure maybe a struct/class like Dictionary.
Use AnyHashable as Key to replace String if in Dictionary.

Swinject - Ambiguous reference to member

I am using Swinject in my Swift 3 app. When I tried
let container = Container()
container.register(NetworkModeling.self) { _ in Network() }
I get an error saying
Ambiguous reference to member 'register(_:name:factory:)'
What is wrong here?
I faced the same issue and i think compiler could be a bit more verbose in this case.
Anyway, my problem was on my side, not in Swinject
Check the following:
NetworkModeling and Network are visible in scope of your registration (they are public, or internal in the same module. remember, that swift3 introduced fileprivate and many other specifiers, so make sure your identifiers are visible to registeting code
Make sure that Network conforms to NetworkModeling. Being unable to see inheritance, swift compiler raises error about ambigous types for Swinject factory
Hope, this helps

Cannot pass dictionary to function in swift

Tearing my hair out on this one.
I'm trying to pass a dictionary of type [String:UIView] to a function in swift that's expecting a [NSObject:AnyObject] :
NOTE: I get the exact same error if I use NSString instead of String:
Any ideas what I'm doing wrong?
opponentImageView is a UIImageView....
PROBLEM RESOLVED
turns out the issue was actually with the 'options' argument being passed 0. Passing NSLayoutFormatOptions(0) made this misleading error go away. Here is what the code now looks like:
It builds fine now...
There is some problem when you try to cast String as NSObject implicitly in pure swift class. You need to define it explicitly
let viewsDict:[NSObject:AnyObject] = ["yourview":view]
and there is one error more options can not be 0.So define options as proper NSLayoutFormatOptions type.

Error: 'class name' redeclared as different kind of symbol?

I was facing the same error as asked in this question
I overcome with this error by solution of declaring class ahead of time in my .h file with the class parameter
I am having FFTBufferManager.h and FFTBufferManager.cpp file and using it in HomeView.h and HomeView.mm file
class FFTBufferManager,CAStreamBasicDescription,DCRejectionFilter;
But now I am having error as
#include "FFTBufferManager.h"
#include "aurio_helper.h"
#include "CAStreamBasicDescription.h"
class CAStreamBasicDescription,FFTBufferManager; //here it shows this error
EXpected Unqualified-id befor ',' token
#interface HomeView
{
FFTBufferManager* fftBufferManager;
//it shows erros
EXpected Unqualified-id befor ',' token
ISO c++ forbids declaration of FFTBufferManager with no type
}
#property FFTBufferManager* fftBufferManager;
//shows error
'FFTBufferManager' is not a type
I'm gathering you're using both C++ and Objective-C.
I'd suggest renaming all your .cpp and .m files in which Objective-C and C++ code are meeting to use the extension .mm - this tells the compiler to use "Objective-C++" rules, and will stop a lot of compiler troubles.
Also, it seems CAStreamBasicDescritpion is a C++ class - you'll have to forward-declare it with class CAStreamBasicDescritpion;, not #class CAStreamBasicDescritpion; (note, no "at" sign) - the second form is only for forward-declaring Objective-C classes. This I suspect is the root cause of the particular error you have observed.
EDIT in response to comment: I'm not sure about your first new issue - that should work fine so long as both FFTBufferManager and CAStreamBasicDescription are C++ classes. As to your second one, depending on where exactly that line of code is (CAStreamBasicDescription thruFormat;) you may need to include the header rather than just the forward-declare: you're declaring an instance of CAStreamBasicDescription here, and the compiler needs to know its structure to do so.
You can't declare more than one class at a time.
Change your declarations to
class CAStreamBasicDescription;
class FFTBufferManager;
The compiler is looking for an unqualified-id because it believes that you're declaring a variable of type CAStreamBasicDescription, so it expects a variable name where you gave it a comma.
Looks like you are trying to create a class that already exists in one of the Cocoa frameworks.