以下代码不起作用:
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true, selector:"caseInsensitiveCompare")
并给出以下错误: 'NSInvalidArgumentException',原因:'不支持的NSSortDescriptor选择器:caseInsensitiveCompare'
似乎在Objective-C中工作正常。关于为什么会出现这种情况的任何想法?
编辑:使用Core Data / SQLite似乎无法做到这一点。在这种情况下,获取数组并在之后的代码中对它进行排序是最好的选择吗?
答案 0 :(得分:37)
Swift 3 - 5.0语法
public sealed partial class MainPage : Page
{
public List<String> List;
public List<String> List2;
private object PreviousFocusItem;
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
List = new List<string> { "data1_GridView1", "data2_GridView1", "data3_GridView1" };
List2 = new List<string> { "data1_GridView2", "data2_GridView2", "data3_GridView2" };
myGridView.ItemsSource = List;
myGridView2.ItemsSource = List2;
rootGrid.GotFocus += Grid_GotFocus;
rootGrid.LostFocus += Grid_LostFocus;
base.OnNavigatedTo(e);
}
private void Grid_LostFocus(object sender, RoutedEventArgs e)
{
PreviousFocusItem = e.OriginalSource;
}
private void Grid_GotFocus(object sender, RoutedEventArgs e)
{
//get the previous focus Element and current focus Element.
var previous = PreviousFocusItem as UIElement;
var current = e.OriginalSource as UIElement;
//got focus logic
if ((!IsElementInsideGridView(myGridView, previous)) &&IsElementInsideGridView(myGridView,current))
{
//gridView got focus as a whole, put your codes here:
output.Text += "Got Focus+1 \n";
}
//lost focus logic
if ((!IsElementInsideGridView(myGridView, current)) &&(IsElementInsideGridView(myGridView,previous)) )
{
//gridView lost focus as a whole, put your codes here:
output.Text += "Lost Focus+1 \n";
}
}
private bool IsElementInsideGridView(GridView gridView,UIElement element)
{
Point topLeft = gridView.TransformToVisual(this).TransformPoint(new Point());
Rect rectBounds = new Rect(topLeft.X, topLeft.Y, gridView.ActualWidth, gridView.ActualHeight);
IEnumerable<UIElement> hits = VisualTreeHelper.FindElementsInHostCoordinates(rectBounds, element);
if (hits == null || hits.Count() == 0)
{
return false;
}
else
{
return true;
}
}
}
答案 1 :(得分:8)
选择器为...
,带冒号,您可以将其与Core Data一起使用。
冒号就在那里,因为该方法(它是caseInsensitiveCompare:
的实例方法)接受一个参数(要与之比较的字符串)。
作为可与Core Data和SQLite一起使用的排序选择器列表 商店可以找到 核心数据编程指南中的Persistent Store Types and Behaviors。
答案 2 :(得分:5)
Swift 5,SwiftUI语法
self._contacts = FetchRequest(
entity: Contact.entity(),
sortDescriptors: [
NSSortDescriptor(key: "name", ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))
],
predicate: predicate
)