2015-08-07 29 views
11

Spero che nessuno mi riferisca a RTFM perché ho avuto a che fare con questo problema da qualche tempo qui e su Apple Developer e facendo molte ricerche.SWIFT: come si crea un predicato con un valore Int?

sto ottenendo un errore EXC-BAD-ACCESS su questa affermazione:

var thisPredicate = NSPredicate(format: "(sectionNumber == %@"), thisSection) 

thisSection ha un valore di 1 Int e visualizza il valore 1 quando mi passa il mouse su di esso. Ma nella zona di debug vedo questo:

thisPredicate = (_ContiguousArrayStorage ...) 

Un altro predicato utilizzando una stringa mostra come ObjectiveC.NSObject Perché succede questo?

+0

Hai una parentesi aperta in più che non dovrebbe essere lì –

+0

Io non sono sicuro perché si vede una parentesi in più sopra, perché questo non è quello che ho - che è: var thisPredicate = NSPredicate (formato: "(sectionNumber ==% @)", thisSection) – PatriciaW

+0

@PatriciaW il problema nella tua domanda è stato che hai perso la parentesi di chiusura dopo ** @ ** . A proposito, ho modificato la mia risposta precedente, forse dovresti dare un'occhiata ad alcune considerazioni che ho aggiunto. –

risposta

17

Si potrebbe provare l'interpolazione di stringa Swift Standard Library Reference. Che sarebbe simile a questa:

let thisSection = 1 
let thisPredicate = NSPredicate(format: "sectionNumber == \(thisSection)") 
+0

Grazie mille.Sono molto inesperto in quest'area e questo ha risolto questo problema. – PatriciaW

12

Sarà necessario cambiare %@ per %i e rimuovere la parentesi in più:

problema principale è che si sta mettendo un Int dove è in attesa di un String.

Ecco un esempio sulla base di questo post:

class Person: NSObject { 
    let firstName: String 
    let lastName: String 
    let age: Int 

    init(firstName: String, lastName: String, age: Int) { 
     self.firstName = firstName 
     self.lastName = lastName 
     self.age = age 
    } 

    override var description: String { 
     return "\(firstName) \(lastName)" 
    } 
} 

let alice = Person(firstName: "Alice", lastName: "Smith", age: 24) 
let bob = Person(firstName: "Bob", lastName: "Jones", age: 27) 
let charlie = Person(firstName: "Charlie", lastName: "Smith", age: 33) 
let quentin = Person(firstName: "Quentin", lastName: "Alberts", age: 31) 
let people = [alice, bob, charlie, quentin] 


let thisSection = 33 
let thisPredicate = NSPredicate(format: "age == %i", thisSection) 

let _people = (people as NSArray).filteredArrayUsingPredicate(thisPredicate) 
_people 

Un'altra soluzione potrebbe essere quella di rendere il valore s' thisSection un String, questo può essere ottenuto String interpolazione o tramite description proprietà del Int .. permette di dire:

Cambio:

let thisPredicate = NSPredicate(format: "age == %i", thisSection) 

per

let thisPredicate = NSPredicate(format: "age == %@", thisSection.description) 

o

let thisPredicate = NSPredicate(format: "age == %@", "\(thisSection)") 

naturalmente, si può sempre saltare questo passaggio e andare per qualcosa di più hardcoded (ma anche corretta) come:

let thisPredicate = NSPredicate(format: "sectionNumber == \(thisSection)") 

Ma tener conto che per qualche strana ragione Interpolazione stringa (questo tipo di struttura:"\(thisSection)") dove conduce per mantenere i cicli come dichiarato here

+0

Hugo, grazie per questi suggerimenti. Ho provato% @ ma non sapevo di usare .description. Ho provato% d ma non% i. – PatriciaW

+0

'.description' è una proprietà di ogni tipo di dati in ** Swift **, trasforma il valore di' Number', 'Date', ecc. Nella sua rappresentazione' String'. '% i' esegui questa conversione per te. –