NSPredicate with NSDictionary, NSArray, JSON & HTML 数据
NSPredicate with NSDictionary, NSArray, JSON & HTML data
我想使用 NSPredicate 在 JSON 数据中获取特定数据。我不知道我什么时候错了,我的 NSLog arrayBDD 在没有 NSPredicate 的情况下工作...
这是我的代码:
NSData *dataBDD = [NSData dataWithContentsOfURL:urlBDD];
NSDictionary *dicoBDD = [NSJSONSerialization JSONObjectWithData:dataBDD options:kNilOptions error:nil];
NSArray *arrayBDD = [[dicoBDD objectForKey:@"post"] objectForKey:@"content"];
NSLog(@"%@", arrayBDD);
NSPredicate *Predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] '<a href='http://www.bundoransurfco.com/wp-content/uploads/'"];
NSArray *arrayPictures = [arrayBDD filteredArrayUsingPredicate:Predicate];
NSLog(@"%@", arrayPictures);
NSInteger count = [arrayPictures count];
for (int i = 0; i < count; i++){}
NSLog(@"%zd", count);
if (count == 0) {
NSLog(@"0");
}
else if (count!=0){
if (count==1){
NSLog(@"1");
}
if (count==2){
NSLog(@"2");
}
if (count==3){
NSLog(@"3");
}
else{
NSLog(@"too much");
}
}
我的 ArrayBDD 是:
<p><strong style="font-size: 13px;">5th March</strong></p>
<p>Swell is 3 foot to 4 foot at the peak and cross shore. Streedagh is worth a look for those wanting a beachie</p>
<p><span id="more-113"></span></p>
<p>High tide: 1811 4.0m <span style="color: #ff0000;"> <a href="http://www.bundoransurfco.com/webcam/"><strong>CLICK HERE FOR LIVE PEAK WEBCAM</strong></a></span></p>
<p>Low Tide: 1134 0.6m</p>
<p><b>3 day forecast to March 7th</b></p>
<p>Good swell all week but the winds are changing. Keep in an eye for gaps in the wind.</p>
<style type='text/css'>
#gallery-1 {
margin: auto;
}
#gallery-1 .gallery-item {
float: left;
margin-top: 10px;
text-align: center;
width: 50%;
}
#gallery-1 img {
border: 2px solid #cfcfcf;
}
#gallery-1 .gallery-caption {
margin-left: 0;
}
/* see gallery_shortcode() in wp-includes/media.php */
</style>
<div id='gallery-1' class='gallery galleryid-113 gallery-columns-2 gallery-size-medium'><dl class='gallery-item'>
<dt class='gallery-icon portrait'>
<a href='http://www.bundoransurfco.com/wp-content/uploads/2014/11/11014854_10152577025667000_5213326955989933628_n.jpg'><img width="225" height="300" src="http://www.bundoransurfco.com/wp-content/uploads/2014/11/11014854_10152577025667000_5213326955989933628_n-225x300.jpg" class="attachment-medium colorbox-113 " alt="11014854_10152577025667000_5213326955989933628_n" /></a>
</dt></dl>
<br style='clear: both' />
</div>
<p><a href="http://www.bundoransurfco.com/webcam/"> </a></p>
<h1> Wind Charts</h1>
<p><a href="http://www.windguru.cz/int/index.php?sc=103244"><img class="size-thumbnail wp-image-747 alignleft" title="wind guru" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/wind-guru-67x68.jpg" alt="" width="67" height="68" /></a> <a href="http://www.xcweather.co.uk/"><img class="alignnone size-thumbnail wp-image-749" title="xcweathersmall" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/xcweathersmall2-67x68.jpg" alt="" width="67" height="68" /></a> <a href="http://www.buoyweather.com/wxnav6.jsp?region=UK&program=nww3BW1&grb=nww3&latitude=55.0&longitude=-8.75&zone=0&units=e"><img class="alignnone size-thumbnail wp-image-750" title="buoy weather" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/buoy-weather-67x68.jpg" alt="" width="67" height="68" /></a> <a href="http://www.windguru.cz/int/index.php?sc=103244">Wind Guru</a> <a href="http://www.xcweather.co.uk/">XC Weather</a> <a href="http://www.buoyweather.com/wxnav6.jsp?region=UK&program=nww3BW1&grb=nww3&latitude=55.0&longitude=-8.75&zone=0&units=e">Buoy Weather</a></p>
<h1>Swell Charts</h1>
<p><a href="http://magicseaweed.com/Bundoran-Surf-Report/50/"><img class="alignnone size-thumbnail wp-image-753" title="msw logo" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/msw-logo-67x43.jpg" alt="" width="75" height="43" /></a> <a href="http://magicseaweed.com/UK-Ireland-MSW-Surf-Charts/1/"><img class="alignnone size-thumbnail wp-image-754" title="magicseaweedwamchart" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/magicseaweedwamchart1-67x68.png" alt="" width="67" height="68" /></a> <a href="http://www.marine.ie/Home/site-area/data-services/marine-forecasts/wave-forecasts"><img class="alignnone wp-image-755 size-thumbnail" title="marine institute irish bouy data" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/marine-institute-irish-bouy-data-67x42.jpg" alt="" width="67" height="42" /></a> <a href="http://magicseaweed.com/Bundoran-Surf-Report/50/">Magic Seaweed</a> <a href="http://magicseaweed.com/UK-Ireland-MSW-Surf-Charts/1/">MSM WAM</a> <a href="http://www.marine.ie/Home/site-area/data-services/marine-forecasts/wave-forecasts">Marine Institute</a></p>
<h1>Pressure, Weather, Tides</h1>
<p><a href="http://news.bbc.co.uk/weather/forecast/13000"><img class="alignnone size-thumbnail wp-image-756" title="bbc pressure" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/bbc-pressure-67x68.jpg" alt="" width="67" height="68" /></a> <a href="http://www.met.ie/"><img class="alignnone size-thumbnail wp-image-759" title="met eireann" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/met-eireann-67x68.jpg" alt="" width="67" height="68" /></a> <a href="http://news.bbc.co.uk/weather/forecast/13000">BBC Pressure</a> <a href="http://www.met.ie/">Met Eireann</a> <a href="http://www.irishtimes.com/weather/tides.html">Irish Tide Tables</a></p>
这会使我的应用程序崩溃:'NSInvalidArgumentException', reason: 'Unable to parse the format string "SELF beginswith[c] '<a href='http://www.bundoransurfco.com/wp-content/uploads/'"'
那么我怎样才能让我的 NSPredicate 工作呢?
'NSInvalidArgumentException', reason: 'Unable to parse the format string "SELF beginswith[c] '<a href='http://www.bundoransurfco.com/wp-content/uploads/'"'
问题出现是因为在NSString
中搜索'
。这就是为什么你必须使用:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", @"<a href='bundoransurfco.com/wp-content/uploads/"];
这个谓词(有效)导致另一个问题:
reason: '-[__NSCFString filteredArrayUsingPredicate:]: unrecognized selector sent to instance
这意味着实际上 arrayBDD
([[dicoBDD objectForKey:@"post"] objectForKey:@"content"]
) 是 NSString
而不是 NSArray
。
您误解了 JSON.
的结构
所以:
NSString *contentString = [[dicoBDD objectForKey:@"post"] objectForKey:@"content"];
既然你想知道<a href='http://www.bundoransurfco.com/wp-content/uploads/
出现了多少次,我推荐这个问题Number of occurrences of a substring in an NSString?。
要获得 "arrayPictures" 之类的副本,您可以查看以下问题:
Using NSRegularExpression to extract URLs on the iPhone 并修改模式以满足您的特定需求。
我想使用 NSPredicate 在 JSON 数据中获取特定数据。我不知道我什么时候错了,我的 NSLog arrayBDD 在没有 NSPredicate 的情况下工作...
这是我的代码:
NSData *dataBDD = [NSData dataWithContentsOfURL:urlBDD];
NSDictionary *dicoBDD = [NSJSONSerialization JSONObjectWithData:dataBDD options:kNilOptions error:nil];
NSArray *arrayBDD = [[dicoBDD objectForKey:@"post"] objectForKey:@"content"];
NSLog(@"%@", arrayBDD);
NSPredicate *Predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] '<a href='http://www.bundoransurfco.com/wp-content/uploads/'"];
NSArray *arrayPictures = [arrayBDD filteredArrayUsingPredicate:Predicate];
NSLog(@"%@", arrayPictures);
NSInteger count = [arrayPictures count];
for (int i = 0; i < count; i++){}
NSLog(@"%zd", count);
if (count == 0) {
NSLog(@"0");
}
else if (count!=0){
if (count==1){
NSLog(@"1");
}
if (count==2){
NSLog(@"2");
}
if (count==3){
NSLog(@"3");
}
else{
NSLog(@"too much");
}
}
我的 ArrayBDD 是:
<p><strong style="font-size: 13px;">5th March</strong></p>
<p>Swell is 3 foot to 4 foot at the peak and cross shore. Streedagh is worth a look for those wanting a beachie</p>
<p><span id="more-113"></span></p>
<p>High tide: 1811 4.0m <span style="color: #ff0000;"> <a href="http://www.bundoransurfco.com/webcam/"><strong>CLICK HERE FOR LIVE PEAK WEBCAM</strong></a></span></p>
<p>Low Tide: 1134 0.6m</p>
<p><b>3 day forecast to March 7th</b></p>
<p>Good swell all week but the winds are changing. Keep in an eye for gaps in the wind.</p>
<style type='text/css'>
#gallery-1 {
margin: auto;
}
#gallery-1 .gallery-item {
float: left;
margin-top: 10px;
text-align: center;
width: 50%;
}
#gallery-1 img {
border: 2px solid #cfcfcf;
}
#gallery-1 .gallery-caption {
margin-left: 0;
}
/* see gallery_shortcode() in wp-includes/media.php */
</style>
<div id='gallery-1' class='gallery galleryid-113 gallery-columns-2 gallery-size-medium'><dl class='gallery-item'>
<dt class='gallery-icon portrait'>
<a href='http://www.bundoransurfco.com/wp-content/uploads/2014/11/11014854_10152577025667000_5213326955989933628_n.jpg'><img width="225" height="300" src="http://www.bundoransurfco.com/wp-content/uploads/2014/11/11014854_10152577025667000_5213326955989933628_n-225x300.jpg" class="attachment-medium colorbox-113 " alt="11014854_10152577025667000_5213326955989933628_n" /></a>
</dt></dl>
<br style='clear: both' />
</div>
<p><a href="http://www.bundoransurfco.com/webcam/"> </a></p>
<h1> Wind Charts</h1>
<p><a href="http://www.windguru.cz/int/index.php?sc=103244"><img class="size-thumbnail wp-image-747 alignleft" title="wind guru" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/wind-guru-67x68.jpg" alt="" width="67" height="68" /></a> <a href="http://www.xcweather.co.uk/"><img class="alignnone size-thumbnail wp-image-749" title="xcweathersmall" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/xcweathersmall2-67x68.jpg" alt="" width="67" height="68" /></a> <a href="http://www.buoyweather.com/wxnav6.jsp?region=UK&program=nww3BW1&grb=nww3&latitude=55.0&longitude=-8.75&zone=0&units=e"><img class="alignnone size-thumbnail wp-image-750" title="buoy weather" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/buoy-weather-67x68.jpg" alt="" width="67" height="68" /></a> <a href="http://www.windguru.cz/int/index.php?sc=103244">Wind Guru</a> <a href="http://www.xcweather.co.uk/">XC Weather</a> <a href="http://www.buoyweather.com/wxnav6.jsp?region=UK&program=nww3BW1&grb=nww3&latitude=55.0&longitude=-8.75&zone=0&units=e">Buoy Weather</a></p>
<h1>Swell Charts</h1>
<p><a href="http://magicseaweed.com/Bundoran-Surf-Report/50/"><img class="alignnone size-thumbnail wp-image-753" title="msw logo" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/msw-logo-67x43.jpg" alt="" width="75" height="43" /></a> <a href="http://magicseaweed.com/UK-Ireland-MSW-Surf-Charts/1/"><img class="alignnone size-thumbnail wp-image-754" title="magicseaweedwamchart" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/magicseaweedwamchart1-67x68.png" alt="" width="67" height="68" /></a> <a href="http://www.marine.ie/Home/site-area/data-services/marine-forecasts/wave-forecasts"><img class="alignnone wp-image-755 size-thumbnail" title="marine institute irish bouy data" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/marine-institute-irish-bouy-data-67x42.jpg" alt="" width="67" height="42" /></a> <a href="http://magicseaweed.com/Bundoran-Surf-Report/50/">Magic Seaweed</a> <a href="http://magicseaweed.com/UK-Ireland-MSW-Surf-Charts/1/">MSM WAM</a> <a href="http://www.marine.ie/Home/site-area/data-services/marine-forecasts/wave-forecasts">Marine Institute</a></p>
<h1>Pressure, Weather, Tides</h1>
<p><a href="http://news.bbc.co.uk/weather/forecast/13000"><img class="alignnone size-thumbnail wp-image-756" title="bbc pressure" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/bbc-pressure-67x68.jpg" alt="" width="67" height="68" /></a> <a href="http://www.met.ie/"><img class="alignnone size-thumbnail wp-image-759" title="met eireann" src="http://www.bundoransurfco.com/wp-content/uploads/2010/12/met-eireann-67x68.jpg" alt="" width="67" height="68" /></a> <a href="http://news.bbc.co.uk/weather/forecast/13000">BBC Pressure</a> <a href="http://www.met.ie/">Met Eireann</a> <a href="http://www.irishtimes.com/weather/tides.html">Irish Tide Tables</a></p>
这会使我的应用程序崩溃:'NSInvalidArgumentException', reason: 'Unable to parse the format string "SELF beginswith[c] '<a href='http://www.bundoransurfco.com/wp-content/uploads/'"'
那么我怎样才能让我的 NSPredicate 工作呢?
'NSInvalidArgumentException', reason: 'Unable to parse the format string "SELF beginswith[c] '<a href='http://www.bundoransurfco.com/wp-content/uploads/'"'
问题出现是因为在NSString
中搜索'
。这就是为什么你必须使用:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", @"<a href='bundoransurfco.com/wp-content/uploads/"];
这个谓词(有效)导致另一个问题:
reason: '-[__NSCFString filteredArrayUsingPredicate:]: unrecognized selector sent to instance
这意味着实际上 arrayBDD
([[dicoBDD objectForKey:@"post"] objectForKey:@"content"]
) 是 NSString
而不是 NSArray
。
您误解了 JSON.
所以:
NSString *contentString = [[dicoBDD objectForKey:@"post"] objectForKey:@"content"];
既然你想知道<a href='http://www.bundoransurfco.com/wp-content/uploads/
出现了多少次,我推荐这个问题Number of occurrences of a substring in an NSString?。
要获得 "arrayPictures" 之类的副本,您可以查看以下问题:
Using NSRegularExpression to extract URLs on the iPhone 并修改模式以满足您的特定需求。