Xamarin.ios Enumerate如何在MonoTouch中工作?
在MonoTouch中,我需要处理NSSet中的每个对象。我尝试使用Enumerate,如下所示:Xamarin.ios Enumerate如何在MonoTouch中工作?,xamarin.ios,nsset,enumerate,Xamarin.ios,Nsset,Enumerate,在MonoTouch中,我需要处理NSSet中的每个对象。我尝试使用Enumerate,如下所示: public override void ReturnResults ( BarcodePickerController picker, NSSet results ) { var n = results.Count; // Debugging - value is 3 results.Enumerate( delegate( NSObject obj, ref bool sto
public override void ReturnResults ( BarcodePickerController picker, NSSet results )
{
var n = results.Count; // Debugging - value is 3
results.Enumerate( delegate( NSObject obj, ref bool stop )
{
var foundCode = ( obj as BarcodeResult ); // Executed only once, not 3 times
if ( foundCode != null )
{
controller.BarcodeScannedResult (foundCode);
}
});
// Etc
}
尽管在结果中使用三个对象调用该方法,但在委托中只处理一个对象。我本以为该委托会被执行三次,但我一定对它的工作原理有错误的想法
找不到任何文档或示例。非常感谢您的建议。您必须将
ref
参数设置为false。这指示处理程序继续枚举:
if ( foundCode != null )
{
controller.BarcodeScannedResult (foundCode);
stop = false; // inside the null check
}
是苹果文档中的ObjC等效项。您必须将
ref
参数设置为false。这指示处理程序继续枚举:
if ( foundCode != null )
{
controller.BarcodeScannedResult (foundCode);
stop = false; // inside the null check
}
是苹果文档中的ObjC等价物。或者您可以尝试此扩展方法以使其更简单
public static class MyExtensions {
public static IEnumerable<T> ItemsAs<T>(this NSSet set) where T : NSObject {
List<T> res = new List<T>();
set.Enumerate( delegate( NSObject obj, ref bool stop ) {
T item = (T)( obj ); // Executed only once, not 3 times
if ( item != null ) {
res.Add (item);
stop = false; // inside the null check
}
});
return res;
}
}
公共静态类MyExtensions{
公共静态IEnumerable itemsA(此NSSet),其中T:NSObject{
List res=新列表();
集合枚举(委托(NSObject obj,ref bool stop){
T item=(T)(obj);//只执行一次,不执行三次
如果(项!=null){
决议增列(项目);
stop=false;//在空检查中
}
});
返回res;
}
}
然后,您可以执行以下操作:
foreach(BarcodeResult foundCode in results.ItemsAs<BarcodeResult>()) {
controller.BarcodeScannedResult (foundCode);
}
foreach(barcodesult在results.ItemsAs()中查找代码){
controller.barcode扫描结果(foundCode);
}
注意:请记住,这会创建另一个列表并将所有内容复制到其中,这会降低效率。我这样做是因为匿名方法中不允许使用“yield return”,而我可以想到的其他方法是使用更多的代码使它成为一个真正的枚举器,而不需要复制。我处理的大多数集合都很小,所以这无关紧要,但是如果你有一个大集合,这就不理想了 或者您可以尝试此扩展方法使其更简单
public static class MyExtensions {
public static IEnumerable<T> ItemsAs<T>(this NSSet set) where T : NSObject {
List<T> res = new List<T>();
set.Enumerate( delegate( NSObject obj, ref bool stop ) {
T item = (T)( obj ); // Executed only once, not 3 times
if ( item != null ) {
res.Add (item);
stop = false; // inside the null check
}
});
return res;
}
}
公共静态类MyExtensions{
公共静态IEnumerable itemsA(此NSSet),其中T:NSObject{
List res=新列表();
集合枚举(委托(NSObject obj,ref bool stop){
T item=(T)(obj);//只执行一次,不执行三次
如果(项!=null){
决议增列(项目);
stop=false;//在空检查中
}
});
返回res;
}
}
然后,您可以执行以下操作:
foreach(BarcodeResult foundCode in results.ItemsAs<BarcodeResult>()) {
controller.BarcodeScannedResult (foundCode);
}
foreach(barcodesult在results.ItemsAs()中查找代码){
controller.barcode扫描结果(foundCode);
}
注意:请记住,这会创建另一个列表并将所有内容复制到其中,这会降低效率。我这样做是因为匿名方法中不允许使用“yield return”,而我可以想到的其他方法是使用更多的代码使它成为一个真正的枚举器,而不需要复制。我处理的大多数集合都很小,所以这无关紧要,但是如果你有一个大集合,这就不理想了 不客气,很高兴我帮了你。请接受答案,这样其他StackOverflow用户就不会认为它没有答案。不客气,很高兴我帮了你。请接受答案,以便其他StackOverflow用户不会将其视为未回答。