/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package shop1;
/**
*
* @author Pc
*/
public class NewJFrame1 extends javax.swing.JFrame {
/**
* Creates new form NewJFrame1
*/
public NewJFrame1() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("jTextField1");
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(77, 77, 77)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(198, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(129, 129, 129))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(84, 84, 84)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 119, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(54, 54, 54))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
print
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
从NSArray上面,我想要的对象有一个单词&#34; cloud&#34;
我试过下面的代码
NSArray *arrData = [NSArray arrayWithObjects:
@"cloud,country,plant",
@"country,cloud,plant",
@"country,plant,cloud",
@"clouds,country,plant"
,@"country,clouds,plant",
nil];
但是它在arrResult中提供了所有5个对象。但我只需要3(0,1,2)个对象。
答案 0 :(得分:2)
尝试:
NSPredicate* predicate = [NSPredicate predicateWithBlock:^(NSString* string, NSDictionary* options){
NSArray* array = [string componentsSeparatedByString:@","];
return [array containsObject:@"cloud"];
}];
答案 1 :(得分:1)
尝试以下代码,
它会起作用,
NSPredicate *hsPredicate = [NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
NSArray *sepretArray =[((NSString*)evaluatedObject) componentsSeparatedByString:@","];
NSPredicate *subPredicate = [NSPredicate predicateWithFormat:@"self == %@",@"cloud"];
return ([sepretArray filteredArrayUsingPredicate:subPredicate].count > 0);
}];
NSArray *arrResult = [arrData filteredArrayUsingPredicate:hsPredicate];
答案 2 :(得分:1)
这个可以解决问题。但我不知道这是否是有效的方法。
NSArray *arrData = [NSArray arrayWithObjects:
@"cloud,country,plant",
@"country,cloud,plant",
@"country,plant,cloud",
@"clouds,country,plant"
,@"country,clouds,plant",
nil];
NSMutableArray *resArray = [[NSMutableArray alloc]init];
for(NSString *tempString in arrData)
{
NSArray *cache = [tempString componentsSeparatedByString:@","];
if([cache containsObject:@"cloud"])
{
[resArray addObject:tempString];
}
}