I'd appreciate if someone could help! I'm trying to parse the following page of Groupon website http://www.groupon.com/browse/chicago?category=activities-and-nightlife
var webGet = new HtmlWeb();
var deal1 = webGet.Load("http://www.groupon.com/browse/chicago?category=activities-and-nightlife");
I want to get the whole block of each Deal(i.e. offer for discount)
HtmlNodeCollection content_block = deal1.DocumentNode.SelectNodes("//div[#class = 'deal-list-tile grid_5_third']");
Then out of each block i want to get title, company name, location and price.
foreach(HtmlNode node in content_block)
{
string title2 = node.SelectSingleNode("//div[#class = 'deal-title js-permalink']").InnerText;
string country2 = node.SelectSingleNode("//p[#class = 'merchant-name']").InnerText;
string location2 = node.SelectSingleNode("//p[#class = 'location']").InnerText;
string price2 = node.SelectSingleNode("//div[#class = 'price']/span").InnerText;
}
Here i get confused, i need to write all the information about deals into
DbSet<Deal> Deals , but even if i try to display the content as ViewBag.Message = title + country + location + price; i get System.NullReferenceException: Object reference not set to an instance of an object in the line with content_block.
What am i doing wrong =(
Thanks in advance!
The problem appears to be that the selectnodes returns nothing or null when no nodes are found instead of an empty collection. so this means you should probably wrap if (content_block != null) { around your code block above.
Related
I would like to use a variable inside a Firestore reference. I have a sub-collections stored on the database per shop and they all have the format 'menu Shop1' or 'menu Shop2'. I have to store it this way otherwise if I use menu alone, the collectionGroup reference points to all the menus and returns them all at once - which is not what I want.
I'm struggling to pass the name of the shop to the collectionGroup reference.
This does not work:
let shopName = String("Shop1")
let collectionRef = String("menu \(shopName!)")
let ref = db.collectionGroup((collectionRef!))
But then this works:
let ref = db.collectionGroup("menu Shop1")
I have tried all the variations I know and it still wont pass the string. Does anybody know how to fix this? I'm guessing its a small tweek!
I would just concatenate the string like so:
let shopName: String = "Shop1"
let refString: String = "menu " + shopName
let ref = db.collectionGroup(refString)
Don't forget the 'space' after "menu"
You could also simplify further like so:
let shopName: String = "Shop1"
let ref = db.collectionGroup("menu " + shopName)
The argument does not need to be hard coded...
There's no need for specifying 'String'
let shopName = String("Shop1")
Just make it
let shopName = "Shop1"
Then this is not correct if the intention is to actually create a ref to a collection
let collectionRef = String("menu \(shopName!)")
it should be
let shopsCollection = db.collection("shops")
or like this
let shopName = "shop1"
let thisShop = "menu " + shopName
let shopsCollectionGroup = db.collectionGroup(thisShop)
But... I am not sure you're using the collectionGroups correctly to start with based on the names you're using.
A collection group consists of all collections with the same ID, so for example you could have a collection group of 'shops' whereas yours is called 'menu Shop1' which would indicate a single shop. Or from the guide a collectionGroup called 'landmarks' which would include landmarks from multiple cities.
Read though the Collection Group Queries guide again to ensure it's being used correctly.
As a side note, please protect your code by handling optionals properly.
shopName!
is bad news is shopName is nil as it will crash you code. See nil-coelescing operators, guard and if statements.
I'm having problems with an object that returns me in Realm, the strange thing is that if I printo console the object if I start it well but however if I try to access its value it tells me that it is empty.
The structure of the object is as follows:
class Favourite : Object {
var character : Character!
}
I create an object and add it to the DB
let fav = Favourite()
fav.character = character
FavouriteDao.sharedInstance.addFavourite(characterFavourite: fav)
Get all objects of favorite type
func getAllFavourites() -> Results {
return realm.objects(Favourite.self)
}
When I get the item and do a print
Favourite {
character = Character {
name = Spider-Man;
descriptionC = Bitten by a radioactive spider, high school student Peter Parker gained the speed, strength and powers of a spider. Adopting the name Spider-Man, Peter hoped to start a career using his new abilities. Taught that with great power comes great responsibility, Spidey has vowed to use his powers to help people.;
thumbnail = Thumbnail {
id = 815D93D0-C116-4267-978C-9E47C0074D0D;
path = http://i.annihil.us/u/prod/marvel/i/mg/3/50/526548a343e4b;
extensionImage = jpg;
};
};
If I try to access the character element it tells me that it is nil
Somebody manages to understand because if I make a print of the favorite object it shows me that there is inside a character object but nevertheless if I try to accede to it it says that it does not exist?
What you do is totally wrong from the very beginning. You should read the realm docs first. https://realm.io/docs/swift/latest/#getting-started
For example.
class Favourite : Object {
var character : Character!
}
is not something you should do in Realm.
Assuming your Character is well-defined, the code should be dynamic var character : Character? = nil at least.
I've been learning iOS development for the past three weeks, I'm currently following a course on Udemy so far so good.
However I'm following one of the lectures whereby we build an Instagram Clone.
The instructor is using three arrays which are as follows:
var usernames = [""] // Stores all usernames
var userIds = [""] // Stores all Id's of the given usernames
var isFollowing = [false] // Stores where or not you're following that user
To me trying to keep track of what userId goes with what username using two arrays is basically an accident waiting to happen so I decided to set off and find a more feasible approach. I reverted back to my .Net days and decided to create a list so I went and created a class as follows:
class Users{
var Username : NSString = ""
var UserId : NSString = ""
var Following : Bool = false
}
Now inside my ViewController I make a call to Parse which returns me a list of users and I'm basically trying to loop through the response, and add them to the list class as shown here:
var t = [Users]() // After googling the web, this seems to be the syntax for a list declaration ?
let u = Users()
for object in users{
if let o = object as? PFUser {
u.Username = o.username!
u.UserId = o.objectId!
u.Following = o.IsFollowing!
self.t.append(u)
}
}
print(self.t)
Now when I print this to the console I see the following:
ParseStarterProject_Swift.Users
As I have one user at present, however when I try to loop through T and display the username in the console it doesn't display anything.
for x in t {
print(x.Username)
}
Your basic intuition is correct, it's better to have an array of custom objects, not multiple arrays.
Regarding making it more Swifty, consider your Users type. You might want something like:
struct User {
let username: String
let userId: String
let following: Bool
}
Note,
property names should start with lowercase letter;
Users should probably be called User, as it represents a single user;
we don't generally initialize values to default values like that, but rather specify them in the initializer;
we probably use String not NSString;
if a property cannot change, you'd use let, not var;
properties begin with lower case letters;
Then you can do something like:
var t = [User]()
for object in users {
if let o = object as? PFUser {
t.append(User(username: o.username!, userId: o.objectId!, following: o.IsFollowing!)
}
}
print(t)
Clearly, with all of those ! forced unwrapping operators, you'd want to be confident that those fields were populated for all of those properties.
Using struct is nice because (a) it's a value type; (b) you get the initializer for free; and (c) you can just print them. If you really wanted User to be a reference type (a class), you'd do something like:
class User {
let username: String
let userId: String
let following: Bool
init(username: String, userId: String, following: Bool) {
self.username = username
self.userId = userId
self.following = following
}
}
And if you wanted to be able to just print them, you'd define it to conform to CustomStringConvertible:
extension User: CustomStringConvertible {
var description: String { return "<User; username = \(username); userId = \(userId); following = \(following)>" }
}
With the class, you can feel free to change that description computed property to show it in whatever format you want, but it illustrates the idea.
You are correct in considering that keeping track of what userId goes with what username using two arrays is dangerous, you in the correct direction with your approach.
First, I would just like to suggest that you use correct naming convention:
Classes should be singular (except in very specific cases).
Variable/property names should begin with lowercase.
This would mean that your user class should look like this:
class User {
var username : NSString = ""
var userId : NSString = ""
var following : Bool = false
}
I will keep your existing naming use for the next part. The main problem with your code is that the variable "u" is a object which you create only once and then modify it. You should be creating a new "Users" object for each user instead of modifying the original. If you don't do this you will just have an array with the same user multiple times. This is how your code would look now:
var t = [Users]()
for object in users {
if let o = object as? PFUser {
let u = Users()
u.Username = o.username!
u.UserId = o.objectId!
u.Following = o.IsFollowing!
self.t.append(u)
}
}
print(self.t)
Next you mention that when you print to console you see the text: ParseStarterProject_Swift.Users, that is because Swift does not automatically print a pretty text with the content of your object. In order for it to print something more detailed, your "Users" object would need to implement the CustomStringConvertible. You can see a more detailed answer about that here: how-can-i-change-the-textual-representation-displayed-for-a-type-in-swif.
Lastly, you mention that when you loop trough "t" and display the username in the console it does not display anything. This is caused by one of two things:
Because there are no users being returned from parse, so the "t" array is actually empty. Try print(t.count) to see how many objects are in the array.
Because your "Users" object declares an empty string "" as the default username and the username is not being set correctly when getting the data from the parse. Which means that it IS actually printing something, just that it is an empty string. Try defining a different default value like var username : NSString = "Undefined" to see if it prints something.
Good luck learning swift!
This question should be rather simple.
I have the following code:
ViewBag.SenderID = new SelectList(db.Senders, "SenderID", "SenderContactName", survey.SenderID);
//Attempts
string _SenderContactName = ViewBag.SenderID.ToString();
string _senderContactName = survey.SenderID.ToString();
Results:
_SenderContactname = System.Web.MVC.SelectList
_senderContactName = "1"
What I want the result to be is the SenderContactName from the VewBag, in the form of a string.
I Believe my second attempt is closer to the working code. But I canĀ“t seem to figure out how to fix it on my own.
In your case you should do:
string _senderContactName = db.Senders
.FirstOrDefault(x => x.SenderID == survey.SenderID)
.SenderContactName;
I am trying to setup a search in umbraco examine.I have two search fields ,material and manufacturer.when I trying to search with one material and one manufactuere it will give the correct result.but when try to search more than one material or manufacturer it doesn't give the result.here is my code
const string materialSearchFields = "material";
const string manufacturerSearchFields = "manufacturer";
if (!string.IsNullOrEmpty(Request.QueryString["material"]))
{
material = Helper.StripTags(Request.QueryString["material"]);
}
if (!string.IsNullOrEmpty(Request.QueryString["manufacturer"]))
{
manufacturer = Helper.StripTags(Request.QueryString["manufacturer"]);
}
if (!string.IsNullOrEmpty(Request.QueryString["material"]) || !string.IsNullOrEmpty(Request.QueryString["manufacturer"]))
{
var query = userFieldSearchCriteria.Field(materialSearchFields, material).And().Field(manufacturerSearchFields, manufacturer).Compile();
contentResults = contentSearcher.Search(query).ToList();
}
here my search keywors in querystring is material=iron,steel
how can we split this keyword and search done?
Thanks in advance for the help....
You are using the AND operator, in your case I think you are looking for the GROUPEDOR instead?
I was just working in an old project and grabbed this snipet from there (which I've adapted for your needs). I think it's going to help you:
public IEnumerable<DynamicNode> SearchUmbraco(string[] keywords, string currentCulture)
{
// In this case I had some diferent cultures, so this sets the BaseSearchProvider to the given culture parameter. You might not need this, use your default one.
BaseSearchProvider searcher = SetBaseSearchProvider(currentCulture);
var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
var groupedQuery = searchCriteria.GroupedOr(new[] {"manufacturer", "material"}, keywords).Compile();
var searchResults = searcher.Search(groupedQuery);
// ... return IEnumerable of dynamic nodes (in this snipet case)
}
I just split(etc) the keywords in an helper and pass them to a string array when I call this method.
Just check this infomation on the umbraco blog: http://umbraco.com/follow-us/blog-archive/2011/9/16/examining-examine.aspx