logo
down
shadow

error: generic parameter 'T' could not be inferred in swift


error: generic parameter 'T' could not be inferred in swift

Content Index :

error: generic parameter 'T' could not be inferred in swift
Tag : ios , By : John Phipps
Date : November 15 2020, 03:01 PM

I hope this helps you . I have function which make flatted array from nested array. It works perfectly for int array. , You have to tell the compiler the type of T.
func makeFlatArrayGeneric<T>(_ array: [Any]) -> [T] {
    var flatArray = [T]()
    for item in array {
        if let item = item as? T {
            flatArray.append(item)
        } else if let item = item as? [Any] {
            let result: [T] = makeFlatArrayGeneric(item)
            flatArray += result
        }
    }
    return flatArray
}

let array: [Any] = [1, 2, [3], [4, [5]]]
let items: [Int] = makeFlatArrayGeneric(array) //[1, 2, 3, 4, 5]
extension Array {
    func flatten() -> Array<Element> {
        return self.flatMap({ element -> [Element] in
            if let array = element as? Array {
                return array.flatten()
            } else {
                return [element]
            }
        })
    }
}

let items: [Int] = array.flatten() //[1, 2, 3, 4, 5]

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Swift custom flatMap implementation causes error Generic parameter could not be inferred


Tag : swift , By : user171555
Date : March 29 2020, 07:55 AM
hope this fix your issue Something does not conform to NSObject.
Swift 4 automatically infers constraints for the generic parameter U used in your flatMap function by looking at all parameters which include U. U is used as a generic parameter to Wrapper which requires U: NSObject. Something is not a subclass of NSObject.

Initializing a Struct in Swift Error: Generic parameter could not be inferred


Tag : ios , By : Timbo
Date : March 29 2020, 07:55 AM
may help you . For a generic type, the generic parameter is actually considered to be part of the type. So the type of your struct isn't MyStruct, it's MyStruct (or whatever generic type you use. Therefore, you can't just declare the type by itself if there's not any information for the compiler to use to infer the actual type. Instead, you have to include the generic type in your declaration:
var myStruct: MyStruct?

Generic parameter 'A' could not be inferred (Swift 4, Bond and ReactiveKit frameworks migrating from Swift 2)


Tag : swift , By : Rob
Date : March 29 2020, 07:55 AM
With these it helps If you ever migrated from old versions of ReactiveKit/Bond implemented syntax, here is how to do it for your UIKit Reactive Extensions:
import UIKit
import Bond
import ReactiveKit

extension ReactiveExtensions where Base: UISearchBar {

    var delegate: ProtocolProxy {
        return base.protocolProxy(for: UISearchBarDelegate.self, setter: NSSelectorFromString("setDelegate:"))
    }

    var text: DynamicSubject<String?> {

        let selector = #selector(UISearchBarDelegate.searchBar(_:textDidChange:))
        let textChanged = delegate.signal(for: selector) { (subj: SafePublishSubject<Void>, _: UISearchBar, _: NSString) in
            subj.next(())
        }

        return dynamicSubject(
            signal: textChanged,
            get: { $0.text },
            set: { $0.text = $1 }
        )
    }
}

extension UISearchBar: BindableProtocol {

    public func bind(signal: SafeSignal<String?>) -> Disposable {
        return reactive.text.bind(signal: signal)
    }
}

Generic parameter 'Self' could not be inferred swift


Tag : swift , By : tayles
Date : January 12 2021, 08:33 AM
hope this fix your issue 1) You have a comma that shouldn't be there. Also, you could use some spacing for readability, and you don't need the semicolon (though Swift will let you put it there):
print("ISBN: " + self.currentISBN! + "  ID:" + book_id! + " Title:" + book_title!)
print("ISBN: \(self.currentISBN!)  ID:\(book_id!) Title:\(book_title!)")

Generic parameter 'Self' could not be inferred (swift 4)


Tag : xcode , By : OlioEngr
Date : March 29 2020, 07:55 AM
With these it helps range(of / index(of returns an optional. You have to unwrap the optional in the range expression
extension String
{
    func substringToFirstChar(of char: Character) -> String?
    {
        guard let pos = self.range(of: String(char))?.lowerBound else { return nil }
        // or  guard let pos = self.index(of: char) else { return nil }
        let subString = self[..<pos]
        return String(subString)
    }
}
extension String
{
    func substringToFirstChar(of char: Character) -> String
    {
        guard let pos = self.range(of: String(char))?.lowerBound else { return self }
        // or  guard let pos = self.index(of: char) else { return self }
        let subString = self[..<pos]
        return String(subString)
    }
}
Related Posts Related QUESTIONS :
  • Swift circular corners doesn't work properly on different screen sizes
  • How change .navigationBarTitle font in SwiftUI?
  • What happens if `MPSCNNConvolutionDataSource` protocol's `load` method return false?
  • Why does the UIBarButtonItem not show up when I create it with code?
  • How to prevent following view controller inheriting navigation view controller bar?
  • What does it mean when .xcodeproj files are shown in red in Xcode?
  • Xcode SwiftUI how to scroll the Preview Canvas?
  • Expand tableview and increase scrollview content size by an appropriate difference on clicking a button inside a table c
  • xcodebuild failed with return code: 65 - using CLI/Appcenter but no error messages
  • "'isDeveloperModeEnabled' is deprecated: This no longer needs to be set during development. " What is isDevelo
  • Right click in UIKit for Mac
  • How we Support Dark Mode in Current iOS App with iOS 13?
  • Security vulnerablility issue in ios mobile app
  • How to set a background color for the viewController in swiftUI?
  • SwiftUI : Segue navigation
  • iOS 13 Custom UISearchBar _searchField crash
  • USDZ models have holes in SceneKit but not QuickLook
  • Why does iOS Password Autofill not prompt to save password on keychain?
  • Pushing information to firebase after event is created(Firebase-Realtime-Database)
  • Xcode 11 Beta and Carthage bootstrap
  • How to change the status bar background color and text color on iOS 13?
  • Using Core Data with SwiftUI
  • How should I do to wait all the data downloaded completely and show on TableView?
  • Can we specify the number of rows that can be shown in each section of a collection view
  • Why sometimes completion handler not called?
  • Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements
  • In UIKit we can set label.numberOfLines = 0 when we do not know how many lines the text will take? What is the alternati
  • Swift ISO8601 format to Date returning fatal error
  • SwiftUI Login Page Layout
  • Xcode: Could not locate installed application. Install claimed to have succeeded, but application could not be found on
  • See the TestFlight crash log in the Xcode
  • Why does zero UIView's CALayer.speed stops UIAlertController's buttons from working?
  • Firestore collectionGroup query returns empty result
  • Modify text in SwiftUI
  • CNCopyCurrentNetworkInfo with iOS 13
  • WWDC 2019 - iOS13 April 2020 requirement
  • Nativescript IPhone X - How to "hide" home indicator behind two swipes to go to home screen
  • Should I use a singleton location manager in Swift?
  • Accessibility problem in reusable TableViewCell
  • Remove top line containing Done button from Keyboard Using Storyboard or in Swift
  • Swift Storyboard - Centring an Icon and multi-line text horizontally
  • viewDidAppear is delayed by 0.5 sec
  • Pass URL values to SecondView Controller WebView (WKWebView)
  • How to upload images faster on iOS, when using takePictureAsync?
  • Tableview multiple sections with one floating header
  • UITextView: Huge Memory Usage
  • How to put a logo in NavigationView in swiftui?
  • How can I load an UIImage into a SwiftUI Image asynchronously?
  • How to remove highlight on tap of List with SwiftUI?
  • how to use customized toolbar in multiple viewcontroller without repeating the code and copying the elements (swift)?
  • iPhone 7 simulator is not found on Xcode 11 beta
  • SwiftUI view is in the middle instead of in the top
  • How to get iOS 12 Autofill to ask to save password in React Native app?
  • Custom font none installed - Xcode 11 beta
  • How to run iOS app in background forever?
  • Is there a way to create a Dropdown-Menu/button in SwiftUI?
  • What is the use of "Preview Assets" assets catalog in Xcode 11 beta?
  • App delegate methods aren't being called in iOS 13
  • Accessing modifiers of Image in custom View in SwiftUI
  • How to make view the size of another view in SwiftUI
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com