我在根词典中有以下plist条目:
我把它加载到一个可变字典中:
var TabRecordKey = "TabRecord"
var TabRecordID: AnyObject = []
let resultDictionary = NSMutableDictionary(contentsOfFile: path)
println("Loaded DrinksData.plist file is --> \(resultDictionary?.description)")
var myDict = NSDictionary(contentsOfFile: path)
if var dict = myDict {
//loading values
TabRecordID = dict.objectForKey(TabRecordKey)!
所有可爱的,我可以将对象追加到TabRecordID,保存它们,在控制台中打印出来。
但是现在我想过滤我的TabRecordID只显示项目,比如TabRecord [i] [0] =“Homer”
我试过了:
for i in 0..<TabRecord.count {
filter(TabRecordID) {TabRecord[i][0] = "Homer"}
}
但是因为TabRecordID是AnyObject,Swift回复:
通用参数“S”不能绑定到非@objc协议类型 'AnyObject'
我试试:
let filteredArray = TabRecordID[i].filter() { TabRecordID[i][0] == "Homer"}
但是得到:
'AnyObject'没有名为'filter'的成员
我试试:
let filteredArray = (TabRecordID[i] as! Array).filter() { TabRecordID[i][0] == "Homer"}
但是得到:
无法使用类型为“(() - &gt; _)”的参数列表调用“过滤器”
我知道我在这里错过了关于Swift中类型转换的基本内容,但它让我精神恍惚,我无法在这里找到适用的相关问题。帮助一个新人,有人,并告诉我我的方式的错误。善待,我真的在这里尝试......
答案 0 :(得分:1)
试试这个
<Window x:Class="Test_TreeWithDesignData.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test_TreeWithDesignData"
xmlns:pf="clr-namespace:System.ComponentModel;assembly=PresentationFramework"
Title="MainWindow"
Height="250"
Width="325"
mc:Ignorable="d"
>
<Window.DataContext>
<local:DummyViewModel />
</Window.DataContext>
<Grid Name="RootGrid">
<Grid Name="TreeGrid" DataContext="{Binding Persons}">
<TreeView
d:DataContext="{d:DesignData Source=./DesignTimeTreeData.xaml}"
ItemsSource="{Binding}"
>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Style.Triggers>
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource Self}, Path=(pf:DesignerProperties.IsInDesignMode)}"
Value="true"
>
<Setter Property="IsExpanded" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate
DataType="{x:Type local:Person}"
ItemsSource="{Binding Childs}"
>
<StackPanel Orientation="Horizontal" Height="25">
<Label Content="{Binding Name}"/>
<Label Content="{Binding Age}" Margin="3,0,0,0"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Grid>
</Window>
这将记录
let pridicate = NSPredicate(block: { (obj, dic) -> Bool in
let array = obj as! NSArray
let str = array.firstObject as! String
return str=="Homer"
})
let filterdResult = TabRecordID.filteredArrayUsingPredicate(pridicate)
println(filterdResult)
举个例子,你可以在实际情况下使用可选的unwarpping