2015-09-19 13 views
42

Di seguito utilizzato per lavorare a Swift 1.2:Swift 2.0: il tipo di espressione è ambiguo senza più contesto?

var recordSettings = [ 
    AVFormatIDKey: kAudioFormatMPEG4AAC, 
    AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue, 
    AVEncoderBitRateKey : 320000, 
    AVNumberOfChannelsKey: 2, 
    AVSampleRateKey : 44100.0] 

Ora, si dà l'errore:

"Type expression is ambiguous without more context".

risposta

56

Si potrebbe dare il compilatore ulteriori informazioni:

let recordSettings : [String : Any] = 
[ 
    AVFormatIDKey: kAudioFormatMPEG4AAC, 
    AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue, 
    AVEncoderBitRateKey : 320000, 
    AVNumberOfChannelsKey: 2, 
    AVSampleRateKey : 44100.0 
] 
+1

Grazie per questo. Non sapevo che fosse disponibile. Stavo provando AnyObject e quello non ha funzionato. Purtroppo il ricevitore di recordSettings richiede [String: AnyObject] – lernerbot

+1

@lernerbot, vedere la mia risposta di seguito. – Stephan

+1

Sei corretto. La mia domanda iniziale era vaga. – lernerbot

35

Per rispettare per il formato richiesto [String : AnyObject] richiesto dal parametro recordSettings; Oltre a @ risposta di Unheilig, è necessario convertire il vostro ints e floats-NSNumber:

let recordSettings : [String : AnyObject] = 
[ 
    AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC), 
    AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue as NSNumber, 
    AVEncoderBitRateKey : 320000 as NSNumber, 
    AVNumberOfChannelsKey: 2 as NSNumber, 
    AVSampleRateKey : 44100.0 as NSNumber 
] 
+3

questa dovrebbe essere la risposta accettata – DCDC

+2

Sono d'accordo tranne che non ho fatto la domanda correttamente. @Stefano ha risposto alla domanda che intendevo porre. – lernerbot

3

Ho anche avuto questo messaggio di errore tentando di inizializzare una serie di optional con nil:

var eggs : [Egg] = Array<Egg>(count: 10, repeatedValue: nil) 

Expression Type 'Array<Egg>' is ambiguous without more context.

Modifica [Egg] al [Egg?] corretto l'errore.