将Set转换为Array?

时间:2017-03-12 17:04:54

标签: ios swift core-data nsset

当通过关系从CoreData中检索对象集合时,swift正在给我一个NSSet而不是像我期望的那样的数组。

有没有办法可以将集合转换为数组?

代码:

var updateExercise : UserExercise?

destinationViewController?.userExerciseSets = self.updateExercise?.exercisesets as? [UserExerciseSet]

警告是

  

从'NSSet'演员?到不相关的类型'[UserExerciseSet]'总是失败

目标VC具有var:var userExerciseSets : [UserExerciseSet]?

1 个答案:

答案 0 :(得分:8)

您应该如下定义NSManagedObject模型:

# LSTM with dropout for sequence classification 
import numpy
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import LSTM
from keras.preprocessing import sequence,text
from keras.layers.embeddings import Embedding
import pandas as pd


# fix random seed for reproducibility
numpy.random.seed(7)

#fetching sms spam dataset
url = 'https://raw.githubusercontent.com/justmarkham/pydata-dc-2016-tutorial/master/sms.tsv'
sms = pd.read_table(url, header=None, names=['label', 'message'])

#binarizing
sms['label_num'] = sms.label.map({'ham':0, 'spam':1})
sms.head()

X = sms.message
y = sms.label_num
print(X.shape)
print(y.shape)

###################################
tk = text.Tokenizer(nb_words=200, lower=True)
tk.fit_on_texts(X)

x = tk.texts_to_sequences(X)

print len(tk.word_counts)

###################################
max_len = 80
print "max_len ", max_len
print('Pad sequences (samples x time)')

x = sequence.pad_sequences(x, maxlen=max_len)



max_features = 200
model = Sequential()
print('Build model...')

model = Sequential()
model.add(Embedding(max_features, 128, input_length=max_len, dropout=0.2))
model.add(LSTM(128, dropout_W=0.2, dropout_U=0.2))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='rmsprop')

model.fit(x, y=y, batch_size=500, nb_epoch=1, verbose=1, validation_split=0.2, show_accuracy=True, shuffle=True)

然后当你需要一个数组时,你可以简单地使用数组的构造函数来获取一个数组。

class UserExercise: NSManagedObject {
   @NSManaged var exercises: Set<Exercise>!
}